fork download
  1. using System;
  2.  
  3. namespace ConsoleApplication3
  4. {
  5. namespace ConsoleApplication
  6. {
  7. public class Program
  8. {
  9. public static void Main()
  10. {
  11. double d = 1.0;
  12. d -= 0.3;
  13. d -= 0.2;
  14. d += 0.2;
  15. d += 0.2;
  16. d += 0.2;
  17. d += 0.2;
  18. d += 0.2;
  19.  
  20. Console.WriteLine("Standard formatting: {0}", d);
  21. Console.WriteLine("Internal Representation: {0:r}", d);
  22. Console.WriteLine("Console WriteLine 0 decimals: {0:0}", d);
  23. Console.WriteLine("0 decimals Math.Round: {0}", Math.Round(d, MidpointRounding.AwayFromZero));
  24. Console.WriteLine("15 decimals then 0 decimals Math.Round: {0}", Math.Round(Math.Round(d, 15, MidpointRounding.AwayFromZero), MidpointRounding.AwayFromZero));
  25. }
  26. }
  27. }
  28. }
  29.  
Success #stdin #stdout 0.04s 33952KB
stdin
Standard input is empty
stdout
Standard formatting: 1.5
Internal Representation: 1.4999999999999998
Console WriteLine 0 decimals: 2
0 decimals Math.Round: 1
15 decimals then 0 decimals Math.Round: 2