Sample Input
-33.8670522
151.1977362
12.9582505
Required Output
-33.86
151.19
12.95
Output that i am getting
-33.87
151.20
12.96
Below is my code but issue is it is rounding off the last digit
$n_va = substr($va, 0, $va < 0 ? 3 : 2);
Can anybody help me with it!!!!
Advertisement
Answer
To get your desired results, multiply the numbers by 100, take the intval
to truncate to the integer part and then divide by 100 again:
$values = array(-33.8670522, 151.1977362, 12.9582505); foreach ($values as $value) { echo intval($value * 100) / 100 . PHP_EOL; }
Output:
-33.86 151.19 12.95