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.
Related
I have variables of bitcoin values all rounded to 8 decimal places. eg
1.00645600
I need a way in jQuery or php to get the whole number [1], The decimal values [006456], and trailing zeros [00]. I have already tried php substr but it messed up with the results since im dealing with variables.
Simple and general solution in PHP without involving regular expressions (that is an option also):
$number = '1.00645600';
$flooredNumber = floor($number); // 1
$decimalPart = (string) (floatval($number) - $flooredNumber); // 0.006456
$decimals = str_replace('0.', '', $decimalPart); // 006456
$trailingZeros = str_replace(rtrim($number, '0'), '', $number); // 00
substr
Returns the portion of string specified by the start and length parameters.
http://php.net/manual/en/function.substr.php
If the numbers in your string are always in the same position you can use substr() to get the desired values:
$str = '1.00645600';
echo substr($str, 0, 1)."\r\n";
echo substr($str, 2, 2)."\r\n";
echo substr($str, 2, 6)."\r\n";
Output:
1
00
006456
Perhaps, this way?
<?php
$i = '1.00645600';
echo rtrim(rtrim($i, '0'), '.');
?>
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
I have a string, its content is "24896". Now I want to add some zeros to the left, so I tried:
$test = str_pad($myString, 4, "0", STR_PAD_LEFT);
The result is "24896" again, no zeros added to the left. Am I missing something here?
Thanks!
The second argument to str_pad() takes the full length of the final string; because you're passing 4 and the length of $myString is 5, nothing will happen.
You should choose a width that's at least one bigger than your example value, e.g.:
str_pad($myString, 9, '0', STR_PAD_LEFT);
// "000024896"
Update
This might be obvious, but if you always want 4 zeros in front of whatever $myString is:
'0000' . $myString;
Because you're padding it to length 4, and your string 24896 is 5 characters long, hence it doesn't need to pad anything as it's already more than 4 characters long.
The second parameter in the str_pad function is the new length of the string.
Try
$myString = "24896" ;
$test = str_pad($myString, strlen($myString) + 4, "0", STR_PAD_LEFT);
echo $test;
Output
000024896
Just make the pad first and attach it, presuming you don't know how long it is. No need to calculate the length of the original string:
$x = 4;
$pad = str_pad('', $x, '0');
$test = $pad.$myString;
Or better
$x = 4;
$test = str_pad('', $x, '0').$myString;
The length you specified in the str_function is less than the input string read documentation properly
try this it will work for you
Your String is 5 character
e.g $myString=24896;
Now you want to add 5 zero to the left
then your length will be you string + 5 the actual is 5+5=10;
Now pass this to the function your function will be like this
$test = str_pad($myString, 10, "0", STR_PAD_LEFT);
echo $test;
OUTPUT:
0000024896
how many no zero you want to add? no zeros added because padding length is smaller than your given $myString length.
Please try this one
$number = 24896;
$number = sprintf('%06d', $number);
echo $number;
or use this one
$number = 24896;
$number = str_pad($number, 6, '0', STR_PAD_LEFT);//here 6 is padding length
echo $number;
output
024896
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