I have a number stored in a variable. The number is pulled from the database as 9900..I need to convert this number to 99.00 in order to display to a customer in HTML.
I was wondering how can I achieve this in php.
Thanks.
$priceInCents = 9900;
$priceInDollars = $priceInCents / 100;
And then you can use round() or number_format() as you please.
You can use the number_format() function for that:
echo number_format(9900/100);
You can do this by either using money_format or number_format, if you are going to display it as a price you should look at money_format, otherwise, number_format is the way to go.
Related
I have a number that is pulled from an API that displays as 6.5e-7, I would like to display this as 0.00000060 (I think this is what 6.5e-7 means),
I have tried to get this correct using the php ROUND function but it will only show as 0 or 6.5e-7 when I used a precision.
print round($vSat, 8);
Any help would be much appreciated
If I had to guess, I would think you need to do a formatted print which is printf
printf ("%.8f", $vSat)
%.8f means floating point and 8 decimal places
http://us3.php.net/printf
Use printf/sprintf formatting.
printf("%10.10f", $yournumber);
I have a code like this $new_amount = round(($old_amount/53),2);
amount may be 1,2, ...,10000;
How get exact $old_amount from $new_amount; Now i tried to use
$old_amount = ceil($new_amount);` AND `$old_amount = floor($new_amount);
but not working properly; please help me;
You can't, once you round a value you get rid of the extra precision. This precision is lost. However, the old value is still stored in the $old_amount variable.
If you need the exact amount, don't change the $old_amount while creating $new_amount. So you can use $old_amount later.
There is no way to back. Both Ceil() and floor() functions can't give you what you want.
I am having trouble thinking of a php function to add zeros after a decimal. Let say I have $money="10000" I need a function that will add .00 to 10000.00 and to add just a zero to 0 after 234.5.
can anyone help me please?
Just as Dan Grossman said; number_format is your friend here and you'd accomplish those double zeros by:
$money = "1234";
$formatedNumber = number_format($money, 2, '.', '');
echo $formatedNumber;
// 1234.00
The function is number_format
http://php.net/manual/en/function.number-format.php
Always search the PHP manual first :)
Maybe not the answer you want to hear, but I think you're better off storing these values in numeric types and formatting them properly just before output
I currently do:
$totalBill = number_format($totalBill, 2);
$totalBill = '$'.$totalBill;
This does not handle cases though where the total is say 10 cents. It returns "$0.1" which I do not want. Does anyone have a better way?
$totalBill = sprintf('$%.2f',$totalBill);
You probably want money_format() instead
Use PHP's money_format function.
I have a set of integers that I would like to change to a US money format.
Here are some examples of what I would like to do. The first number is what is in the database and the second is what I would like the money format to look like.
4500 = $45.00
395 = $3.95
19000 = $190.00
I'm really just unsure what function I should be using to make this sort of conversion.
Thank you for the help
You could use number_format()
number_format((4500/100), 2);
There's also money_format(), but it's slightly more complicated.
Take a look at money_format, which formats a number as a currency string.
$number=4500;
$money='$'.round($number/100,2);
Have you looked at money_format()? You'll just need to divide your values by 100 before using them w/this function.
Also, you could consider number_format() as well.