Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add zeros to the left of a number [duplicate]

Tags:

php

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 ?

like image 956
Arsinux Avatar asked Dec 10 '25 05:12

Arsinux


1 Answers

try this

echo str_pad($base_three, 6, "0", STR_PAD_LEFT);
like image 83
Javad Avatar answered Dec 11 '25 17:12

Javad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!