What class to use for money representation?

Never use a floating point number to represent money. Floating numbers do not represent numbers in decimal notation accurately. You would end with a nightmare of compound rounding errors, and unable to reliably convert between currencies. See Martin Fowler’s short essay on the subject. If you decide to write your own class, I recommend basing … Read more

Matlab vs C++ Double Precision

You got confused by the different ways C++ and MATLAB are printing double values. MATLAB’s format long only prints 15 significant digits while C++ prints 17 significant digits. Internally both use the same numbers: IEEE 754 64 bit floating point numbers. To reproduce the C++-behaviour in MATLAB, I defined a anonymous function disp17 which prints … Read more

Set precision for a float number in PHP

You can use number_format() to achieve this: echo number_format((float) $number, $precision, ‘.’, ”); This would convert 1518845.756789 to 1518845.757. But if you just want to cut off the number of decimal places short to 3, and not round, then you can do the following: $number = intval($number * ($p = pow(10, $precision))) / $p; It … Read more

Why does floating-point arithmetic not give exact results when adding decimal fractions?

Binary floating point math is like this. In most programming languages, it is based on the IEEE 754 standard. The crux of the problem is that numbers are represented in this format as a whole number times a power of two; rational numbers (such as 0.1, which is 1/10) whose denominator is not a power … Read more

What is the purpose of max_digits10 and how is it different from digits10?

To put it simple, digits10 is the number of decimal digits guaranteed to survive text → float → text round-trip. max_digits10 is the number of decimal digits needed to guarantee correct float → text → float round-trip. There will be exceptions to both but these values give the minimum guarantee. Read the original proposal on … Read more

Precision in C floats

“6 digits after the decimal point” is nonesnse, and your example is a good demonstration of this. This is an exact specification of the float data type. The precision of the float is 24 bits. There are 23 bits denoting the fraction after the binary point, plus there’s also an “implicit leading bit”, according to … Read more