3

I have a basic index.php page with some variables that I want to print in several places - here are the variables:

<?php
  $firstprice = 1.50;
  $secondprice = 3.50;
  $thirdprice = 20;
?>

My challenge is that later in the document, when I print, I get the prices without the second '0' in the price - this is what happens:

<?php print "$firstprice";?> // returns 1.5 - not 1.50!

SO - I know how to do this with JS, but how is this done in PHP 5+? Basically I want to print the second '0' if there is already a decimal, so if the variable is equal to '3', it stays as '3', but if it's equal to '3.5' it converts to display '3.50' with a second '0', etc.

Here's a JS example - what's the PHP equivalent?

JS:

.toFixed(2).replace(/[.,]00$/, ""))

Many thanks!!

Jamison
  • 2,218
  • 4
  • 27
  • 31

6 Answers6

10

This is simple and it will also let you tweak the format to taste:

$var = sprintf($var == intval($var) ? "%d" : "%.2f", $var);

It will format the variable as an integer (%d) if it has no decimals, and with exactly two decimal digits (%.2f) if it has a decimal part.

See it in action.

Update: As Archimedix points out, this will result in displaying 3.00 if the input value is in the range (2.995, 3.005). Here's an improved check that fixes this:

$var = sprintf(round($var, 2) == intval($var) ? "%d" : "%.2f", $var);
Jon
  • 428,835
  • 81
  • 738
  • 806
  • This would not account for `.00` obtained through rounding, so I would recommend replacing `.00` in the final result instead of checking the value, or to check the fraction (`$var - floor($var)`) against your rounding threshold, e.g. against `.005` if `$var` is positive, and `0.995` if it is negative. – Arc Apr 13 '11 at 11:25
  • Case in point: Assume you calculate prices with tax added, you can get values with a fraction < 0.01 (unless you cut off the resulting value before output). – Arc Apr 13 '11 at 11:30
  • @Archimedix: Good catch. I fixed that in the shortest possible manner, to keep the solution an one-liner. – Jon Apr 13 '11 at 11:31
5
<?php

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

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

Grumpy
  • 2,140
  • 1
  • 25
  • 38
  • Not quite... this will also print 2 decimals if the number is an integer, or if the first two decimals round to 0. One would still have to eliminate `.00` afterwards, e.g. using `str_replace()`. – Arc Apr 13 '11 at 11:18
3

You could use

   if (is_float($var)) 
   {
     echo number_format($var,2,'.','');
   }
   else
   {
     echo $var;
   }
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1

What about something like this :

$value = 15.2; // The value you want to print

$has_decimal = $value != intval($value);
if ($has_decimal) {
    echo number_format($value, 2);
}
else {
    echo $value;
}


Notes :

  • You can use number_format() to format value to two decimals
  • And if the value is an integer, just display it.
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
0

Alternatively way to print

$number = sprintf('%0.2f', $numbers); 
   // 520.89898989 -> 520.89
ankitr
  • 5,992
  • 7
  • 47
  • 66
S'copion Sam
  • 69
  • 1
  • 2
0

you can use number_format():

echo number_format($firstprice, 2, ',', '.');
strauberry
  • 4,189
  • 5
  • 34
  • 50
  • Thanks! Actually, I changed this slightly to make sure the number formats with a "." instead of a "," to mark the decimal point - it's a difference between the US and EU system for marking numbers - so the result is echo number_format($firstprice, 2, '.', ','); BUT - this still turns $20 into $20.00 - I want to make sure that numbers with no decimal stay that way ... so what's missing here? – Jamison Apr 13 '11 at 11:23
  • Oh I see. For this number_format is not the right coice because it appends ,00 EVERYTIME... – strauberry Apr 13 '11 at 11:30