[英]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?我应该为 C# 使用什么数据类型?
So you're working with floating point values.所以你正在处理浮点值。 You have two options:您有两个选择:
Use Double
if you are given significant digits , eg 2000000.50
actually means, say, 2.00e6
(3 significant digits).如果给定有效数字,请使用Double
,例如2000000.50
实际上意味着,例如, 2.00e6
(3 个有效数字)。 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 ).如果小数点后有有效数字,请使用Decimal
,例如2000000.50
实际上表示2000000.500
(小数点后3 位)。 Usually these kinds of values are from finance: dollars and cents etc.通常这些类型的价值来自金融:美元和美分等。
You should use decimal.你应该使用十进制。
Here is the description of decimal in MSDN:这是MSDN中十进制的描述:
https://msdn.microsoft.com/en-us/library/364x0z75.aspx https://msdn.microsoft.com/en-us/library/364x0z75.aspx
Here is the difference from double:这是与 double 的区别:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/921a8ffc-9829-4145-bdc9-a96c1ec174a5/decimal-vs-double-difference?forum=csharpgeneral https://social.msdn.microsoft.com/Forums/vstudio/en-US/921a8ffc-9829-4145-bdc9-a96c1ec174a5/decimal-vs-double-difference?forum=csharpgeneral
The short answer is: decimal
.简短的回答是: 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
).替代方案 - double
不是货币值的首选,因为美分是二进制编码的,并且值经常被四舍五入(例如,您可能有100.03999999
而不是100.04
)。
We used decimal too.我们也使用了十进制。
decimal = 128-bit precise decimal values with 28-29 significant digits,十进制 = 128 位精确十进制值,具有 28-29 位有效数字,
double = 64-bit double-precision floating point type, double = 64 位双精度浮点类型,
float = 32-bit single-precision floating point type, float = 32 位单精度浮点类型,
int = 32-bit signed integer type int = 32 位有符号整数类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.