3

I can't believe this has actually come to me asking a question. But I have some decimal values in my database ie 0.9999999, using:

number_format($result['debit'], 2);

number_format($result['debit'], 2,".","");

number_format(floor($result['debit']*pow(10,2))/pow(10,2),2); 

I am continually getting the value 1 back from my original 0.999999.

It doesn't seem to matter what I try, it constantly rounds the number. What can I do to simply get 0.99? I am at the point where I am going to explode (currently I mean the function)

Another note is this will need to work with negative numbers.

The Humble Rat
  • 4,442
  • 6
  • 36
  • 71
  • I don't know the language, but could you convert it to a string and then just take the first 4 characters? – Persistence May 19 '15 at 14:35
  • Your third attempt works for me: http://codepad.org/1kqeBrCZ – Jim May 19 '15 at 14:35
  • @JamesHughes I need this to be dynamic as the values change for example: 1456.256325. In this instance I would only get 1456. – The Humble Rat May 19 '15 at 14:36
  • You could use the "." as a control character and only take one more character than that after finding the index of it in the string? As I said I don't know PHP, but that's how I'd do it in one of the .NET languages (although I don't think this is a problem I've ever encountered) – Persistence May 19 '15 at 14:37
  • possible duplicate of [PHP How do I round down to two decimal places?](http://stackoverflow.com/questions/12277945/php-how-do-i-round-down-to-two-decimal-places) – Jim May 19 '15 at 14:37
  • @Jim don't think so, I think the issue is more that it's rounding when he just wants to truncate, but I may have misunderstood. – Persistence May 19 '15 at 14:39
  • @JamesHughes That's what that link will do, it will always round down to 2 decimal places - so `0.999999` becomes `0.99`. – Jim May 19 '15 at 14:40
  • @Jim Oh right, fair enough then – Persistence May 19 '15 at 14:41
  • @JamesHughes You are correct, I simply want to truncate. – The Humble Rat May 19 '15 at 14:43
  • actually @Jim thinking about it, rounding 0.999999 to 2dp is 1.00 – Persistence May 19 '15 at 18:05

1 Answers1

4

number_format will always round, you can however use below code to make it work:

$number = 0.9999999; 
echo number_format(floor($number*100)/100, 2); //Returns 0.99

Note: use floor() for positive numbers and ceil() for negative numbers.

Daan
  • 11,690
  • 6
  • 30
  • 49
  • 1
    @yaa110 That is because 0.29 does not exist as a float number: `printf('%.20f',0.29)` shows you: `0.28999999999999998002` – Daan May 19 '15 at 14:40
  • @Daan please see the result from the following: `number_format(floor(-0.9999999999*100)/100, 2);` this is giving me -1. It works well for positive numbers though. – The Humble Rat May 19 '15 at 14:45
  • @TheHumbleRat Works for negative numbers aswell you just need to change `floor` to `ceil` function. – Daan May 19 '15 at 14:47
  • @Daan so it does. Good man. Many thanks for this. One for the snippets. – The Humble Rat May 19 '15 at 14:53