2

I was using the function number_format() to manage my decimal places, but I'm not getting the result I want. I wish to have up to 6 decimal places, but i don't want every number to display them. For example if my output is 27.54, number_format is gonna give me 27.540000. Is there function or a way to ignore those extra zeros and only display the significant decimals?? Thank you!

inBlackAndWhite
  • 91
  • 4
  • 10

3 Answers3

2
$r = 27.54;
 echo (float) number_format($r,6);
Nouphal.M
  • 6,304
  • 1
  • 17
  • 28
0

If I understand correct, you want up to 6 decimals, but no more and if its less that 6 decimal places to ignore any zeros. If so, just get the floatval of the formated string:

$number = floatval(number_format($number,6))
0

Try using (float) to cast the value back to a float (which will remove the trailing 0's):

$val = number_format(11.11, 6);
echo (float)$val;
Marty
  • 39,033
  • 19
  • 93
  • 162
  • I dont know why, but if I use number_format it erases the zeros in my integers. the $val = (float) $val; doesn't let me limit the decimals, but works great removing the insignificant zeros!! Thank you!! – inBlackAndWhite Dec 10 '13 at 04:15