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.
Related
Using this solution. I am trying to format a number with leading zero's. The length is 12 digits. The starting is 00000000001 and if the number is 10 then 000000000010 and so on. I have tried below
OGP-<?php $model=$dataProvider->getModels()[0];
str_pad($model['OGP_Serial_No'], 12, '0', STR_PAD_LEFT)?>
But it's giving me empty result OGP-.
How can I achieve this?
Any help would be highly appreciated.
Try this
<?php
if(isset($dataProvider->getModels()[0]) && $dataProvider->getModels()[0] != array()){
$model = $dataProvider->getModels()[0];
$OGP_Serial_No = $model['OGP_Serial_No'];
$myNumber = "OGP-".str_pad($OGP_Serial_No, 12, '0', STR_PAD_LEFT);
}else{
$myNumber = "OGP-".str_pad(1, 12, '0', STR_PAD_LEFT);
}
echo $myNumber;
?>
Try check the real content eg: using var_dump()
<?php
$models = $dataProvider->getModels(); [0];
$myString = 'OGP-' . str_pad( $models[0]['OGP_Serial_No'] , 12, '0', STR_PAD_LEFT);
var_dump($myString);
?>
$MarketValue = number_format(100000 * 4, 2, '.',',');
$purchasing_val =number_format(100000 * 6, 2, '.',',');
$pls = $purchasing_val - $MarketValue ;
The Above Code result should be 200000 but its just showing 200, its only occur when i format the values, Where's the error ? Thanks in advance.
You are not supposed to format then calculate.
Try to calculate, then format the number.
$MarketValue = 100000 * 4;
$purchasing_val = 100000 * 6;
$pls = number_format($purchasing_val - $MarketValue, 2, '.', ',');
EDIT:
As you asked, in case you want to show them as formatted, you have many options.
You could just echo them as number_format($purchasing_val, 2, '.', ','); and number_format($MarketValue, 2, '.', ',');
or you could store them as another variable to show later on like
$FormattedMarketValue = number_format($MarketValue, 2, '.', ',');
$Formattedpurchasing_val = number_format($purchasing_val, 2, '.', ',');
And when ever in your script you echo the two variables $FormattedMarketValue and $Formattedpurchasing_val
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);
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.