Converting Integer String to Money Format - php

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.

Related

Convert 6.5e-7 to a formatted number such as 0.00000060

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);

How get original price from rounded value in php?

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.

Negative number with decimals to positive number in PHP

I'm creating an import based on xml. From the XML i get some values wich are negative amounts like -316.65.
Before I save this values to the database I have to convert them to positive amounts. I've tried the following:
$amount = $inkoopfactuur->td[7] * -1;
This worked but, the decimals where gone.
Is there a way to convert this witouth losing the decimals?
Thanks in advance!
There's a better way to do this:
$amount = abs($inkoopfactuur->td[7]);
However, it could be the case that the integer rounding is the result of something else -- there's not enough information in the question to be certain.
You probably have a different locale.
Check localeconv

convert number to decimal in php

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.

In PHP how do you format a number as currency handling cases such as 10 cents equaling "$.1";

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.

Categories