Aadi
Aadi

Reputation: 7109

How to add numbers after decimal using 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?

Upvotes: 6

Views: 20337

Answers (3)

Xenovoyance
Xenovoyance

Reputation: 350

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

Upvotes: 14

Dyppl
Dyppl

Reputation: 12381

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

Upvotes: 1

Dan Grossman
Dan Grossman

Reputation: 52372

The function is number_format

http://php.net/manual/en/function.number-format.php

Always search the PHP manual first :)

Upvotes: 9

Related Questions