I want format number as style '00000' ex:
$a=1 => $a2="00001"
$b=23 => $b2="00023"
How format number in php?
You should use str_pad : https://www.php.net/manual/en/function.str-pad.php
$formattedValue = str_pad($value, 6, "0", STR_PAD_LEFT);
sprintf:
$formattedValue = sprintf('%05s', $value);
0 is the character to pad 5 times. s specifies that it's a string.
use the str_pad function:-
$a2 = str_pad($a, 5, "0", STR_PAD_LEFT);
Related
I am converting the numbers to be used in API
for example:
$total = 100.5;
$purchaseAmt = str_pad($total, 13, "0", STR_PAD_LEFT);
$formattedPurchaseAmt = str_pad(''.($purchaseAmt*100), 12, "0", STR_PAD_LEFT);
it will output 000000010050
Now, how I can do the opposite, if I have 000000010050 i want to get 100.5
Thanks
I think if you round up your float and format it to always have two numbers after the decimal, you could simply do the opposite of what you do, which is divide by 100
$total = round(100.5, 2);
$purchaseAmt = str_pad($total, 13, "0", STR_PAD_LEFT);
$formattedPurchaseAmt = str_pad(''.($purchaseAmt*100), 12, "0", STR_PAD_LEFT);
#reverting the process
$newTotal = (int)formattedPurchaseAmt / 100; //output: 100.5
Test this code:
echo number_format($formattedPurchaseAmt/100, 1, '.');
Maybe it does not suits all cases, so test it well before to use in production.
I need a function that will be convert an int value to string with fixed length.
For example :
5 -> '000005', 5245 -> '005245' ..
If the number is bigger than 999999 it stays as it is:
2548562 -> '2548562'
Is there PHP function to do that, or do I need to create a custom function?
What you want is this
$value = str_pad($value, 6, '0', STR_PAD_LEFT);
This will pad your integer value with 0's as a string. If there are more than 6 digits, the integer will simply stay as is.
Example:
<?php
$value = 5;
$value = str_pad($value, 6, '0', STR_PAD_LEFT);
print($value . "\xA");
$value = 2548562;
$value = str_pad($value, 6, '0', STR_PAD_LEFT);
print($value);
?>
Output:
000005
2548562
You can use this
$resultString = str_pad($value, length , '0' , STR_PAD_LEFT);
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.
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
I need a suggestion on a function for a php counter. Is there any function to have numbers with 5 digit as 00001, or 00123… this number should be not random but have to increase the value of a previous field.
If a number is $n=’00001’ there is a function to increase by one and get 00002 and not 2?
Thanks
F.
$n2 = str_pad($n + 1, 5, 0, STR_PAD_LEFT);
Use str_pad() by adding 0s (third parameter) to the left (fourth parameter) of the old number $n incremented by 1 (first parameter) until the length is 5 (second parameter).
$number = 1;
$number++;
echo str_pad($number, 5, "0", STR_PAD_LEFT); //00002
As an alternative, in case you are interested, you can use sprintf to pad this with 0's up to a certain number fairly easily.
$numbers = array(0,1,11,111,1111,11111,11111);
$padded = array();
foreach($numbers as $num)
$padded[] = sprintf('%1$05d', ++$num);
print_r($padded);
PHP almost always has numerous ways to do the same thing. :)
You need the str_pad() function to add leading zeros to your indexes.
$new_index = str_pad($index, 5, "0", STR_PAD_LEFT);
Where $index your incrementing index in circle, $new_index is your index with leading zeros.