Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php, add trailing zeros if the number is integer

Tags:

php

Simple maths:

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

if $b equals to 123.00 and $c equals to 1 then $a becomes 123.

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

It is possible to test whether $a has any non-zero fraction part or not, and then add the trailing zeros as necessary.

But I am looking for php functions to do the same thing. Possible?

EDIT :

What if I do not want the commas from number_format there ?

like image 549
Istiaque Ahmed Avatar asked Jul 16 '12 18:07

Istiaque Ahmed


1 Answers

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

like image 61
Niet the Dark Absol Avatar answered Oct 18 '22 23:10

Niet the Dark Absol