简体   繁体   中英

C# changing double value 0.0 to 0

The value 0.0 is changing to 0 when I assign it to a double variable. Is there any workaround to preserve the decimal and succeeding 0's if any?

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.

As an alternative (esp. if you are working finance) you can change the type, from double to decimal :

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

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