C#C
C#3y ago
23 replies
n8ta

❔ Convert decimal without trailing 0s

My goal is to print decimal numbers with all available precision and without a trailing .0

decimal a = 123.0m;
decimal b = 123m;
Console.WriteLine(a.ToString()); // 123.0
Console.WriteLine(b.ToString()); // 123
Console.WriteLine(a.ToString() == b.ToString()); // false

I'd like 123.0 to convert to 123 not 123.0. How can I do this for the decimal type? I was somewhat surprised that 123.0 and 123 are converted into different values.
Was this page helpful?