Page 1 of 1
working with decimal values
Posted: Wed Dec 18, 2002 4:03 am
by graziano
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
Posted: Wed Dec 18, 2002 4:10 am
by Takuma
Yeah there is a function for that.
round()
Posted: Wed Dec 18, 2002 8:52 am
by BDKR
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.
Code: Select all
echo number_format(120003.121212121247284784734 , 2, '.', '');
That will echo 120003.12.
Cheers,
BDKR
Posted: Wed Dec 18, 2002 11:45 am
by graziano
Thank you , however number_format is better for me-
Posted: Wed Dec 18, 2002 12:23 pm
by Takuma
I've looked at that function...
Yeah it does round up the number -> change the number.
Sorry for the miss guiding information.
Posted: Wed Dec 18, 2002 1:52 pm
by Rob the R
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.
Code: Select all
<?php
echo round(12.345,2) ;
echo number_format(12.345,2,'.','') ;
?>
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.
Posted: Wed Dec 18, 2002 3:34 pm
by BDKR
Hi Rob,
What you're failing to realize, and the documentation should bear this out, is that number_format does NOT do any rounding! It just formats the number based on the given arguments. Read the documentation.
Cheers,
BDKR
Posted: Wed Dec 18, 2002 4:59 pm
by Rob the R
That's why I asked - the documentation appears to be ambiguous. This quote is from the docs for
number_format():
<?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
?>
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.
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.
Posted: Thu Dec 19, 2002 6:36 am
by BDKR
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