2

I am trying to do a 2 digit precision in PHP Laravel project but it doesnt work. I have the value 1234666.6666667 that I want to make 1234666.66 but all the results I've seen in here or/and in other search pages. This is my code:

$value = 1234666.6666667;
return round($value,2);

any other solution?

  • did, didnt work... –  Oct 02 '17 at 08:54
  • @AlonEitan he wants the result to be `1234666.66` and not `1234666.67` – DarkBee Oct 02 '17 at 08:54
  • oh, should I include `PHP_ROUND_HALF_UP` too? –  Oct 02 '17 at 08:54
  • I dont use laravel, but have you tried sprintf('%.2f')? – Tomm Oct 02 '17 at 08:54
  • yes. I just want to remove the last numbers at the end –  Oct 02 '17 at 08:55
  • @KIKOSoftware I was talking about the second actually because I haven't noticed the part about rounding down, but it's also there in the manual - Don't understand why people don't read it before asking – Alon Eitan Oct 02 '17 at 08:56
  • @AlonEitan: Possibly because folk, in error, think this is obvious. It isn't. – Bathsheba Oct 02 '17 at 08:59
  • *"I just want to remove the last numbers at the end"* - in which case you're treating it as a string so you may as well manipulate is as such; `preg_replace`, or `explode` and trim and glue it back together, or some function of `strpos`, `strlen`, and `substr` – CD001 Oct 02 '17 at 09:04

4 Answers4

4

EDIT:

As I see, you actually want to floor number to 2 decimal points, not to round it, so this answer could help you:

$value = 1234666.6666667;
floor($value * 100) / 100; // returns 1234666.66

If you want 3 decimal points you need to multiple and divide with 1000, for 4 - with 10000 and etc.

You can use number_format, it convert value to string though, so you lose real float value:

$value = 1234666.6666667;
echo number_format($value, 2, '.', ''); // prints 1234666.67

gintko
  • 697
  • 8
  • 16
  • ... that's not actually what the OP wants though, they want `1234666.66` – CD001 Oct 02 '17 at 09:00
  • seems like there's no way how that can be done (at least easy, dont want to complex the code just for such simple thing) so, I ill make it with round –  Oct 02 '17 at 09:03
1

Use this function.

function truncate($i) {
    return floor($i*100) / 100.0;
}

Then you can do

$value = truncate(123.5666666); // 123.56
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
  • Which is actually 123.56000000000000227373675443232059478759765625 – Bathsheba Oct 02 '17 at 09:01
  • @Bathsheba Is there really anyway around that? Floating-point is what it is. – Leo Aso Oct 02 '17 at 09:05
  • You just need to be aware of it when displaying any floating point values back to the user. Excel does it beautifully. Really though, for an easy life just use a decimal type. – Bathsheba Oct 02 '17 at 09:09
0

A pragmatic way is to use round($value - 0.05, 2), but even that gets you into hot water with some edge cases. Floating point numbers just don't round well. It's life I'm afraid. The closest double to 1234666.66 is

1234666.65999999991618096828460693359375

That's what $value will be after applying my formula! Really, if you want exact decimal precision, then you need to use a decimal type. Else use integer types and work in multiples of 100.

For the former choice, see http://de2.php.net/manual/en/ref.bc.php

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0
  $value = bcadd($value, 0, 2); // 1234666.6666667 -> 1234666.66

Another more exotic way to solve this issue is to use bcadd() with a dummy value for the $right_operand of 0, This will give you 2 number after decimal.

Arun
  • 1,609
  • 1
  • 15
  • 18