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!
Asked
Active
Viewed 1,457 times
2
-
Take a look at this (possible duplicated): http://stackoverflow.com/a/5429945/722135 – Babblo Dec 10 '13 at 03:46
-
rtrim doesn't work for me, since it erases the zeros of my integers... my 25000 turns 25... – inBlackAndWhite Dec 10 '13 at 04:11
3 Answers
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))
-
this works great!! but only works when I'm dividing...when I'm multiplying I get 1.48 * 1000 = 1 .... – inBlackAndWhite Dec 10 '13 at 03:54
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