How to add numbers after decimal using php - php

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

Related

I need comma in between figures in php

I don't know how to display my numbers to have commas and a decimal point in php..
I want 12345.90 to be displayed as 12,345.90
thanks in advance
You should check out number_format function. It will solve your problem.
Try this:
echo number_format(12345.90, 2, '.', ','); // Prints 12,345.90

php input with 1 decimal number

all,
I'm making a shopping cart that only accept input number with 1 decimal place.
The following is piece of code I get from a library (with some modification):
$items['qty'] = trim(preg_replace('/([^0-9\.])/i', '', $items['qty']));
This will accept any number. but I want to make it as only 1 decimal. how to modify the code?
Thanks
this can be done with round function
echo round(153.751, 1); // 153.8
this will help Rounding numbers with PHP
You can remove the decimal places after the first decimal place:
$items['qty'] = trim(preg_replace('/[^0-9\.]+|(?<=\.[0-9])[0-9+]+/', '', $items['qty']));
See it
You can use a regex like ^(\d+.{0,1}\d*)$
But PHP have a concept, the filter, that does a lot of validations for you, http://php.net/manual/en/filter.filters.validate.php
If i understand well the question, you can use number_format
$items['qty'] = number_format(trim(preg_replace('/([^0-9\.])/i', '', $items['qty'])),1,".","");
Hope it helps.
add a line:
$items['qty'] = sprintf('%.1f', $items['qty'])

Reading a float from mySQL

I have two float variables that are equal, I retrieve them by PHP and subtract them. the result equals (7.105427357601E-15) I tried to change to double or to decimal and none of that worked. This is what I use in PHP:
$giftcard_balance = $giftcard['balance'];
$total = $product['price'];
$total -= $giftcard_balance;
echo $total;
Here instead of showing 0, it's showing (7.105427357601E-15)
Anyone can help please?
When dealing with prices and account balances you will find that DECIMAL(6, 2) is much easier to deal with than FLOAT. If you experience the same problem when using DECIMAL datatype please post a specific example.
Using round() or similar is a very bad idea as it is simply masking the problem, not fixing it.
In order to get rid of the float inaccuracy, you can, for example, use number formatting.
$a = 7.105427357601E-15;
$b = number_format($a, 2);
echo $a , "\n", $b , "\n";
Perhaps you're looking for the php intval function.
Or maybe the round function.

PHP formatting number, 35 should be 3500

I'm working with currencies and there are some items that cost say $35, some that are $35.50, etc. I'd like to take these numbers and format them to '3500' and if it's 35.50, it should be 3550.
I've tried
number_format($data['amount'], 2, '', '')
But if it's 35.50 it's 355000.
Any help would be great, thanks!
Did you try to multiply the number by 100?
Think of your math lessons:
echo $data['amount'] * 100;
look at the doc, then try this:
number_format($data['amount'], 2)
with those last two '' you were replacing the decimal separator with nothing and the thousands separator with nothing, which is something i don't believe you intended to do.
update
i see from the comments below that i misread your question.
I tried your code with demo here: http://codepad.org/uNAd84gG
and it seems to be in working order.
try:
number_format($amount, 2, ',', '.')
You can omit the last 2 to just have it behave by your locale
number_format($amount, 2);

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.

Categories