Floats and doubles are limited. They can accurately represent the fractional parts of numbers as the sum of two base fractions. The fractional part can be fully represented as the sum of some rows, such as:
1/2 + 0/4 + 1/8 + 1/16 + ...
Unfortunately, there are a large number of frequently used numbers that can never be fully represented, for example:
1/3, 1/7, 1/9, 5/11, etc.
You should consider whether you can accurately represent the number before worrying about rounding. If this type of accuracy is extremely important, then you need to look for a solution without Float or Non-Double.
There are binary decimal libraries that will accurately perform such calculations. They are usually slower to calculate, because there is no special equipment to speed up their calculations. In addition, they tend to take up more memory because you essentially keep a list of numbers.
After you can present the number you want, you want rounding to be simple. You probably won't even need a rounding solution; because the main reason you are trying to combine in this case is because the float cannot correctly represent your value.
Edwin buck
source share