C
C#2y ago
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
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.
17 Replies
MODiX
MODiX2y ago
Samarichitane#3472
REPL Result: Success
123.0m == 123m
123.0m == 123m
Result: bool
True
True
Compile: 365.481ms | Execution: 23.164ms | React with ❌ to remove this embed.
Servator
Servator2y ago
Both are decimal
n8ta
n8ta2y ago
Not the numeric value the value returned by .ToString()
Servator
Servator2y ago
Why you are comparing values with .ToString() Just use == or .Equals()
n8ta
n8ta2y ago
My goal is to print decimal numbers with all available precision and without a trailing .0
Servator
Servator2y ago
That's a better explanation
n8ta
n8ta2y ago
I'm not actually comparing them that's just to show they are diff. I'll edit the Q.
jalepi
jalepi2y ago
Standard numeric format strings
In this article, learn to use standard numeric format strings to format common numeric types into text representations in .NET.
n8ta
n8ta2y ago
@jalepi None of these have the desired behavior as far as I can tell Currency: prints $ so won't work Decimal: No decimal places I want them all Exp: Sci notation (no) FixedPoint: I want varying point based on available precision General: sci notation (no) Number: has , separator (even if I remove that with NumberGroupSeparator="" it still has fixed precision Percent: has % RoundTrip: not supported for decimal Hex: no Here's my very unoptimized string munging soln
var res = value.ToString(CultureInfo.InvariantCulture);
bool didRemoveZeros = false;
while (res.EndsWith("0"))
{
res = res.Remove(res.Length - 1);
didRemoveZeros = true;
}

if (didRemoveZeros && res.EndsWith("."))
{
res = res.Remove(res.Length - 1);
}
return res;
var res = value.ToString(CultureInfo.InvariantCulture);
bool didRemoveZeros = false;
while (res.EndsWith("0"))
{
res = res.Remove(res.Length - 1);
didRemoveZeros = true;
}

if (didRemoveZeros && res.EndsWith("."))
{
res = res.Remove(res.Length - 1);
}
return res;
`
Servator
Servator2y ago
As long as i checked it can be done but i'll be slow af
Stroniax
Stroniax2y ago
value.ToString().TrimEnd('0'), but this (along with your solution) will chop off the 0 in 70.
n8ta
n8ta2y ago
Oh good point I'll have to revise mine to check for the decimal first decimal point* Still would love a native way to either print without .0 or convert 123.0 into 123 brb lunch
Stroniax
Stroniax2y ago
Think something like this would do, might have to add or remove a char index valueText.IndexOf('.') is int i && i >= 0 ? valueText.Substring(0, valueText.IndexOf('0', i)) : valueText
n8ta
n8ta2y ago
Ultimately going with this
var stringified = value.ToString(CultureInfo.InvariantCulture);

// Regex matches strings ending with "." followed by 1 or more 0s
Regex rx = new Regex("(\\.0+)$");
var match = rx.Match(stringified);
if (match.Length != 0)
{
stringified = stringified.Substring(0, stringified.Length - match.Length);
}
return stringified;
var stringified = value.ToString(CultureInfo.InvariantCulture);

// Regex matches strings ending with "." followed by 1 or more 0s
Regex rx = new Regex("(\\.0+)$");
var match = rx.Match(stringified);
if (match.Length != 0)
{
stringified = stringified.Substring(0, stringified.Length - match.Length);
}
return stringified;
jalepi
jalepi2y ago
a.ToString("F0") // f zero
MODiX
MODiX2y ago
dont#0387
REPL Result: Success
decimal d = 1.23M;
Console.WriteLine($"{d:0}");
decimal d = 1.23M;
Console.WriteLine($"{d:0}");
Console Output
1
1
Compile: 655.541ms | Execution: 39.532ms | React with ❌ to remove this embed.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.