PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I know floating point numbers can be a bit tricky but something as simple as the following should work correctly shouldn't it? It would in Java (OK I haven't tried it it but when I was working in Java I'm sure I had no such problem).
Thanks for the reply. I guess I need to move to integers for money (as cents) to avoid the issue altogether. There seems no elegant solution, so the only way is to use round() every time you do anything with floats.
Thanks yes I could, but I think for money I would be more sure of integers. For other people bcmath might be a good alternative, though one would need to check that it doesn't suffer from similar problems to floats.
it doesn't suffer from any such problems. It uses strings to store the results. The range a number can be for it is abitrary compared to the finite range of an integer or floating point. For financial calculations, it is the better choice.