working with decimal values
Moderator: General Moderators
working with decimal values
Hello ,
I have a numberic variable . It should return only 2 decimal values .
for example if the variable is
12.194720484222222
I need to convert it to
12.19
(reducing decimal values to 2)
or if I have
120003.121212121247284784734
I need to convert it to
120003.12
(reducing decimal values to 2)
Is there any way to do that ? I searched for a php function but I was not able to find it .
Thanks
I have a numberic variable . It should return only 2 decimal values .
for example if the variable is
12.194720484222222
I need to convert it to
12.19
(reducing decimal values to 2)
or if I have
120003.121212121247284784734
I need to convert it to
120003.12
(reducing decimal values to 2)
Is there any way to do that ? I searched for a php function but I was not able to find it .
Thanks
Takuma, that's incorrect. Round will change the value! That's not what he wants. What he wants is to reduce the percision of a float or decimal value to two places.
So Graziano, check out number_format(). The manual is your friend.
Anyways, this code right here will do what you want.
That will echo 120003.12.
Cheers,
BDKR
So Graziano, check out number_format(). The manual is your friend.
Anyways, this code right here will do what you want.
Code: Select all
echo number_format(120003.121212121247284784734 , 2, '.', '');Cheers,
BDKR
I can't say I see the difference between number_format(n,2,'.','') and round(n,2) in this case. Don't they both result in a number that's rounded to the second decimal place? The only difference I can see is that number_format() returns a string and round() returns a float.
both return "12.35", correct?
I would also expect round() to take less processing time than number_format(), but it may be small enough to be negligible.
Code: Select all
<?php
echo round(12.345,2) ;
echo number_format(12.345,2,'.','') ;
?>I would also expect round() to take less processing time than number_format(), but it may be small enough to be negligible.
That's why I asked - the documentation appears to be ambiguous. This quote is from the docs for number_format():
Even so, if your goal is to not round, you could also use the floor() function by doing something like:
I can't claim that this would be any better than number_format(), but it would achieve the same result.
Notice that the first example does not round, the second example rounding is not applicable, and the third example it does round. This is either a typo in the documentation or it uses some strange logic to round in the right side of the decimal but not on the left side.<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,234
// 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
?>
Even so, if your goal is to not round, you could also use the floor() function by doing something like:
Code: Select all
echo floor($number*100)/100 ;Hi Rob,
Good eye on that. Based on some of the examples I had seen, and the fact that rounding is never mentioned in the documentation, I took it at face value. It's strange that the documentation does NOT state as much. It should.
So Takuma, sorry about telling you that you were wrong when I was mislead myself.
40 lashings perhaps?
Cheers,
BDKR
Good eye on that. Based on some of the examples I had seen, and the fact that rounding is never mentioned in the documentation, I took it at face value. It's strange that the documentation does NOT state as much. It should.
So Takuma, sorry about telling you that you were wrong when I was mislead myself.
40 lashings perhaps?
Cheers,
BDKR