简体   繁体   中英

Datatype for for value of 20 000 000,50 in C#?

If I need to present value that is

20 000 000,50
12 000 000
 2 000 000,75
   100 000,866
18 000 000,42

What datatype for C# should I use?

So you're working with floating point values. You have two options:

  1. Double
  2. Decimal

Use Double if you are given significant digits , eg 2000000.50 actually means, say, 2.00e6 (3 significant digits). Usuallly these kinds of values are from physical world: volts , ampers , meters , etc.

Use Decimal if you are given significant digits after decimal point , eg 2000000.50 actually means 2000000.500 (3 digits after the decimal point ). Usually these kinds of values are from finance: dollars and cents etc.

The short answer is: decimal .

The alternative - double is not preferred for monetary values because the cents are binary encoded and frequently the values are rounded (eg instead of 100.04 you may have 100.03999999 ).

We used decimal too.

decimal = 128-bit precise decimal values with 28-29 significant digits,
double = 64-bit double-precision floating point type,
float = 32-bit single-precision floating point type,
int = 32-bit signed integer type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM