0

Possible Duplicate:
PHP: show a number to 2 decimal places

How can I format an input number to be 0.00 if it has not any value? I tried (double) but it prints 0 only.

Community
  • 1
  • 1
Ahmed Fouad
  • 2,963
  • 10
  • 30
  • 54

6 Answers6

3

Here you go :)

echo number_format($var,2);
raidenace
  • 12,789
  • 1
  • 32
  • 35
1

If you want it to print specific no. of decimal points, use number_format.

  $float_var = number_format($var, 2);
Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
  • 1
    Casting to float won't help. If `$var` has no value, at best you'll get `0`. Not `0.00`. – cHao Oct 11 '12 at 19:53
1
$var = number_format($number, 2, '.', '');

This forces 2 points after the decimal, sets the decimal as a period. You can also forego the last two as it defaults to it;

Note: The third value is your decimal separator, the fourth value is the thousandths separator.

$var = number_format($number, 2);
Drose
  • 116
  • 5
0

if you are trying to format the value i sugest you to use meioMask pluging. So you define your field as number and the pluging do the trick, even if you set "0" for the value

Ewerton
  • 4,046
  • 4
  • 30
  • 56
0

For direct output:

  printf('%0.2f',$var);

Output into variable:

  $outVar = sprintf('%0.2f',$var);

This statemant casts $var type to float and prints with 2 decimal signs

SlyChan
  • 769
  • 4
  • 15
0

maybe you should check it first if the value is not set

if(!isset($variableName))
{
   // then set
   $variableName = "0.00"; // => string

   //or like this
   $variableName = number_format(0,2); // => this result is also string
}
echo "value: ",$variableName;

result

0.00
user1732887
  • 288
  • 1
  • 2
  • 9