How to fix the accuracy of a variable

In c #

double tmp = 3.0 * 0.05;

tmp = 0.15000000000000002

It is connected with money. The value is indeed $ 0.15, but the system wants to round it to $ 0.16. 0.151 should probably be rounded to 0.16, but not 0.15000000000000002

How can I get the correct numbers (e.g. 0.15 or 0.16 if the decimal is large enough).

+3
source share
6 answers

Use a fixed point variable type or a basic decimal floating point type such as Decimal. Floating-point numbers are always somewhat inaccurate, and binary floating-point representations add another level of inaccuracy when converting to / from base 2.

+14
source

decimal, . , , .

, "" , , , . "" (, ) , , float/double ( ) ( ) .

+5

''

0

A decimal , , .

. , decimal , float.

, , Int32 . - , - . 16- 16 . 1.5 16:16 ​​ 0x00018000. .

/sub/mul/div, , mul/div, .

0

What are you facing - the rounding issue that I mentioned earlier in another post

Is it possible to use "System.Currency" in .NET?

And refer to it also rounding

0
source

Source: https://habr.com/ru/post/1697995/


All Articles