简体   繁体   English

C# 将双精度值 0.0 更改为 0

[英]C# changing double value 0.0 to 0

The value 0.0 is changing to 0 when I assign it to a double variable.当我将其分配给双变量时,值 0.0 将更改为 0。 Is there any workaround to preserve the decimal and succeeding 0's if any?是否有任何解决方法来保留小数点和后续的 0(如果有)?

Edit:编辑:

   double dt = 0.0;
   Console.WriteLine(dt);
   //Output:0

You can provide the desired format :您可以提供所需的格式

double dt = 0.0;
Console.WriteLine($"{dt:f1}");

Here f1 stands for 1 digit after decimal point.这里f1代表小数点后1

As an alternative (esp. if you are working finance) you can change the type, from double to decimal :作为替代方案(尤其是如果您从事财务工作),您可以将类型从double更改为decimal

decimal dt = 0.0m;
Console.WriteLine(dt);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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