Php, add trailing zeros if the number of integers

Simple math:

$a=$b/$c; echo $a; 

if $ b is 123.00 and $ c is 1, then $ a becomes 123.

If $ b is 123.50, $ c is 1, $ a is 123.50. But in the first case, I want $ a to be 123.00.

You can check whether $ a has any nonzero fraction or not, and then add trailing zeros if necessary.

But I'm looking for php functions to do the same. Is it possible?

EDIT:

What if i don't need commas from number_format ?

+8
php
source share
4 answers

Use sprintf("%0.2f",$a); . docs

+23
source share

Use the number_format function. If you do not need comma separators, set all four function parameters as follows (the thousands separator is the fourth parameter):

 $number = number_format(1234, 2, '.', ''); 
+6
source share

Yup, for example:

 $a = 123; $answer = number_format($a,"2"); echo $answer; 
+1
source share
0
source share

All Articles