This question already has answers here:
Adding leading 0 in php
(3 answers)
Closed 8 years ago.
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
echo str_pad($base_three, 6, "0", STR_PAD_LEFT);
You can use sprintf to ensure that it always has a total of 6 digits, with leading zeroes:
$base_three = 101;
$padded = sprintf("%06s", $base_three);
echo $padded;
Convert to a string and pad with 0's.
$test = str_pad($base_three, 6, '0', STR_PAD_LEFT);
echo $test;
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:
$number = 010;
sprintf("%06d", $number);
so the complete piece of code would be:
$number = 10; // For Example
$from_base = 10;
$to_base = 3;
$base_three = base_convert ( $number , $from_base , $to_base );
echo sprintf("%06d", $base_three);
or
printf("%06d", $base_three);
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
Related
This question already has answers here:
PHP How do I round down to two decimal places? [duplicate]
(16 answers)
Closed 4 years ago.
Is there any way to do a regex that cuts off a number at a certain point without rounding (simply drops the digits?) say after 4 digits.... It will not be handling negative numbers, EVER. I could have number inputs such as 0.03123 or 1.31, or 10000.98, etc .... What I have written so far as my solution is rounding and not what I'm seeking....
$number = 10000.51999999;
$precision = 4;
echo "<br>";
// grab number before decimal by rounding down the whole number down...
$numberBeforeDecimal = floor($number);
echo "<br>";
// grab the decimal and set the correct precision needed
$n = $number;
intval($n); // 12
$theDecimalPart = explode('.', number_format($n, ($precision)))[1]; // 3430
echo $theDecimalPart; // this is outputting 5200
$theNewValue = $numberBeforeDecimal.".".$theDecimalPart;
explode() the number to get integer and decimal part separated out in an array
Use substr() function to get relevant precision from the decimal part.
Finally, concatenate them back.
Try the following (Rextester DEMO):
$number = 10000.51999999;
$precision = 4;
// separate out the integer and decimal part
$number_str_arr = explode('.', $number);
// concatenate them back
$theNewValue = $number_str_arr[0] . '.' . substr($number_str_arr[1], 0, $precision);
echo $theNewValue; // displays 10000.5199
This question already has answers here:
Zero-pad digits in string
(5 answers)
Number with 0 on the front? [closed]
(2 answers)
Closed 8 years ago.
I am trying to extract part of a number using sustr() but the following is not working:
$num = 012014;
echo substr($num, 0,2);
returns 51
BUT
$num = '012014';
echo substr($num, 0,2);
returns 01
I want it to return 01 can someone help me
Is is not a normal number, when it's prepended with a zero (0). Then it's an octal number
If you treat $num as a string, it'll work.
$num = '012014';
echo substr($num, 0,2);
I guess you must declare the variable as string like this.
$num = '012014';
echo substr( (string)$num, 0, 2 );
Ooops I didn't notice the leading zero. You should just define $num as a string
$num = '0123';
Please try this
$num = 123424;
$num = (string)$num;
echo substr($num, 0,2);
This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
I have an integer that I need to convert into a 4 digits string. I know the integer number is between 1 and 9999. If the number is 4, I want the output string to be "0004". If the number is 134, I need the string output as "0134" and so on.
What would be the shortest most elegant way of achieving this in PHP? Thank you.
I would use sprintf():
$string = sprintf( "%04d", $number);
Using this demo:
foreach( array( 4, 134) as $number) {
$string = sprintf( "%04d", $number);
echo $string . "\n";
}
You get as output:
0004
0134
Try this
$num = 1;
$paddedNum = sprintf("%04d", $num);
echo $paddedNum;
Try this
str_pad($input, 4, "0", STR_PAD_LEFT);
This will work for integer and string both
http://php.net/manual/de/function.str-pad.php
$input = 9;
$str = str_pad($input, 4, "0", STR_PAD_LEFT); //results in 0009
You can use sprintf with the %d option:
$NewString = sprintf( "%04d", $OldNumber);
the 04 tells sprintf how many digits your number should be, and will fill with zeros if it doesn't reach that number.
$num = rand(1,9999);
echo sprintf( "%04d", $num);
Try this.
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 4 years ago.
PHP - Is there a quick, on-the-fly method to test for a single character string, then prepend a leading zero?
Example:
$year = 11;
$month = 4;
$stamp = $year.add_single_zero_if_needed($month); // Imaginary function
echo $stamp; // 1104
You can use sprintf: http://php.net/manual/en/function.sprintf.php
<?php
$num = 4;
$num_padded = sprintf("%02d", $num);
echo $num_padded; // returns 04
?>
It will only add the zero if it's less than the required number of characters.
Edit: As pointed out by #FelipeAls:
When working with numbers, you should use %d (rather than %s), especially when there is the potential for negative numbers. If you're only using positive numbers, either option works fine.
For example:
sprintf("%04s", 10); returns 0010
sprintf("%04s", -10); returns 0-10
Where as:
sprintf("%04d", 10); returns 0010
sprintf("%04d", -10); returns -010
You can use str_pad for adding 0's
str_pad($month, 2, '0', STR_PAD_LEFT);
string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )
The universal tool for string formatting, sprintf:
$stamp = sprintf('%s%02s', $year, $month);
http://php.net/manual/en/function.sprintf.php
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 11 months ago.
I need to convert numbers to have .00 after them, but only if the number is an integer, or it has just 1 number after the decimal point, like so:
1.4 = 1.40
45 = 45.00
34.77 = 34.77
What reg exp to use for this simple case?
You can also use printf or sprintf
printf("%01.2f", '34.77');
$formatted_num = sprintf("%01.2f", '34.77');
number_format($number, 2, '.', '');
Read more at PHP.net. You don't need to determine if a number is an integer or not -- as long as it's a number, it will be formatted to two decimal places.
If you'd like the thousands separator, change the last parameter to ','.
Check out PHP's built-in function number_format
You can pass it a variable and it'll format it to the correct decimal places
$number = 20;
if (is_int($number)) {
$number = number_format($number, 2, '.', '');
}
read number_format
number_format($number, 2, '.', '');
$num = 0.00638835;
$avg = sscanf($num,"%f")[0] /100;
echo sprintf("%.10f", $avg);
result 0.0000638835