PDA

View Full Version : How to cut decimal numbers? Like 2.5767 to 2.5 ?


kaisellgren
07-02-2006, 02:07 PM
Hi,

My problem is how to cut...
Examples:

2 -> 2
3 -> 3
3.2 -> 3.2
4.15 -> 4.1
1.3453464565 -> 1.3
2.0 -> 2
6.0 -> 6

How to do it? I need to keep the first decimal number after the dot, but if the first decimal number is zero 0 like: 5.000345 then it should become 5, not 5.0!

Help?

marek_mar
07-02-2006, 02:17 PM
number_format() (http://www.php.net/manual/en/function.number-format.php)

morongo
07-02-2006, 02:23 PM
you can use printf():

$floatnumber=32.56789;

printf("%2.1f", $floatnumber);

output: 32.5

Or you can use sprintf() like this if you want to store it:

$floatnumber=32.56789;
$floatstring="";

floatstring=sprintf("%2.1f",$floatnumber);

See how the float-format works?