The API I fetch my data with, returns me 8199000000 for the price.
Which should be 81.99 but I should also expect something like 15499000000 which should result into 154.99.
How can I parse that clean and nice?
You can just divide by 1e8 (1 and 8 zeroes).
$price = 8199000000;
$real = $price / 1e8;
echo $real, PHP_EOL; // 81.99
$price = 15499000000;
$real = $price / 1e8;
echo $real, PHP_EOL; // 154.99
Nevermind I should have just used a regex.....
^([0-9]{2}|[0-9]{3})([0-9]{2})0
Related
I've tried casting to float and number_format but float will always round at two and number_format is fixed on the amount of decimals you specify.
So how can I do this like the following conversion
11.2200 -> 11.22
11.2000 -> 11.20
11.2340 -> 11.234
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
This might help, You can use sprintf given by PHP.
You can use float casting
echo (float) 11.2200;
echo "<br/>";
echo (float) 11.2000;
echo "<br/>";
echo (float) 11.2340;
and you have to check number of digits after decimal point and than get value like below :
$val=(float) 11.2000;
if(strlen(substr(strrchr($val, "."), 1))<2){
echo number_format($val,2);
}
You may use the round() function for this.
i-e round(number,precision,mode);
Example:
echo(round(11.2200,2));
Output
11.22
Thanks
Not sure if you need a fix for this anymore, but I just ran into the same problem and here's my solution:
$array = array(11.2200, 11.2000, 11.2340);
foreach($array as $x)
{
// CAST THE PRICE TO A FLOAT TO GET RID OF THE TRAILING ZEROS
$x = (float)$x
// EXPLODE THE PRICE ON THE DECIMAL (IF IT EXISTS)
$pieces = explode('.',$x);
// IF A SECOND PIECE EXISTS, THAT MEANS THE FLOAT HAS AT LEAST ONE DECIMAL PLACE
if(isset($pieces[1]))
{
// IF THE SECOND PIECE ONLY HAS ONE DIGIT, ADD A TRAILING ZERO TO FORMAT THE CURRENCY
if(strlen($pieces[1]) == 1)
{
$x .= '0';
}
}
// IF NO SECOND PIECE EXISTS, ADD A .00 TO IT TO FORMAT THE CURRENCY VALUE
else
{
$x .= '.00';
}
}
I would like to show a number 1000000 in the format 10,00,000 in PHP.
What is the PHP function to do this conversion?
use number_format function
<?php
echo number_format(100000); // prints 1,000,000
?>
If you need to display a value formatted as Lakhs and Crores:
$amount = 1000000000;
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount;
should give 100,00,00,000 instead of 1,000,000,000
I'm new to PHP.
My code reads a price value from a Steam game's json data.
http://store.steampowered.com/api/appdetails/?appids=8870
Problem is that the value of the price node is not formatted with a comma separator for dollars and cents. My code works to piece together the dollars and cents but is it the right way to do it for this instance. Also if there is another easier method of doing my newbie code, feel free to show me where it can be improved. Thanks!
<?php
$appid = '8870';
$ht = 'http://store.steampowered.com/api/appdetails/?appids=' . $appid;
$fgc = file_get_contents($ht);
$jd = json_decode($fgc, true);
$gdata = $jd[$appid]['data'];
$gname = $gdata['name'];
$gprice = $gdata['price_overview']['final'];
$gdesc = $gdata['detailed_description'];
$gusd = substr($gprice, 0, -2);
$gcent = substr($gprice, 2);
echo $gname. '<br>';
echo 'Price: $' .$gusd. ',' .$gcent;
?>
If I may ask another question... can the price data aka $gprice be added to another price data that is fetched, to return a total.
I would essentially do what you are doing except its much simpler to just divide by 100:
Turn the price into a float:
$gprice = $gprice / 100;
and then use money_format
Ref: PHP Docs - money_format
You could also do this, but there isn't really a need.
$gprice = (int) $gdata['price_overview']['final'];
The conversion is not bad, but you could also use this:
$gusd = $gprice/100;
echo $gname. '<br>';
echo 'Price: $' .str_replace('.', ',', $gusd);
or use money_format instead of replace, but it's a little more complicated.
also, to add another you could do it with just the + or += operators like this:
$gprice+= $gdata['price_overview']['final'];
I seem to be having an issue with PHP rounding (sort of) numbers by default, I am calling the eBay API and getting back the price of an item and the postage. I need to add these two numbers before entering them into my database, the issue seems to be when I add the two numbers up they come back with a strange value:
shipping = 5.95
item price = 18.55
If I add the two up I get a result of 23.
$ship = $item->shippingInfo->shippingServiceCost;
$price = $item->sellingStatus->currentPrice;
$sell = ($ship + $price);
Do I need to tell php the returned value is a number?
Make sure both of your values are actually integers or floats, instead of strings. Summing strings to eachother could result in strange behavior.
Try this:
$ship = (float) $item->shippingInfo->shippingServiceCost;
$price = (float) $item->sellingStatus->currentPrice;
$sell = ($ship + $price);
Now $sell should consist of a float. You can use (float) to cast the variable to a floating variable.
Take a look at typecasting in general, as is quite important in a variety of scenarios for you as a PHP developer.
The problem is not the $ship is a string with 5.95 .. that would be properly converted to float but I think it is .. 5,95 and that would be converted to 5
<?php
$ship = "5,95";
$price = "18,55";
$sell = ($ship + $price);
echo "$sell = ($ship + $price)";
?>
output:
23 = (5,95 + 18,55)
convert:
<?php
$ship = (float) "5,95";
$price = (float) "18,55";
$sell = ($ship + $price);
echo "$sell = ($ship + $price)";
?>
output:
23 = (5 + 18)
Use:
$ship = str_replace(',','.',$ship);
$price = str_replace(',','.',$price);
And it should work!
I have these value stored in a decimal 10,2 field
1052730
956700
How do i print this using php so that the value is like
$10,527.30
$9,567.00
basically i am trying to avoid the value as
$1,052,730 <--- this i dont want
You can use the
money_format($format, $value)
function in php. The details of the formatting is given here.
Well, assuming that 1052730 is really 10527.30 as alluded to in your question:
$number = 1052730;
$decimals = $number % 100; //30 in this case
$digits = floor($number / 100);
$paddedDecimals = str_pad($digits, 2, '0', STR_PAD_LEFT);
$out = '$' . number_format($digits, 0).'.'.$paddedDecimals;
echo $out; // $10,527.30
There are no floating point calculations used for the decimal part, so there's no need to worry about precision issues (although at this precision it would likely be hard to get a float error in there)...
Just divide by 100:
<?php
echo number_format(1052730/100, 2, '.', ',') . PHP_EOL;
echo number_format(956700/100, 2, '.', ',') . PHP_EOL;
printf ("$%01.2f", ($input / 100));