I have a piece of code for converting a Decimal number into base 3
$number = 10; // For Example
$from_base = 10;
$to_base = 3;
$base_three = base_convert ( $number , $from_base , $to_base );
echo $base_three;
So the number that it echos is 101 and it has 3 digits. but I what it to echos is 000101 so that it has 6 digits. Converting Decimal into base 3 with always 6 digits even though it has only 3 or 4 useful digits, is my goal! how can I solve it ?
try this
You can use
sprintf
to ensure that it always has a total of 6 digits, with leading zeroes:Convert to a string and pad with 0's.
http://php.net/manual/en/function.str-pad.php
You can use sprintf to make sure you always output 6 digits, whatever number you have:
so the complete piece of code would be:
or
printf formats the variable and echos it, sprintf() doesn't echo but returns it
(s)printf can do a lot more, see http://www.php.net/manual/en/function.sprintf.php