[ACCEPTED]-Remove 0s from the end of a decimal value-decimal
You can also modify the decimal itself so 2 that any ToString() will give you what you 1 want (more details in my answer here) :
public static decimal Normalize(decimal value)
{
return value/1.000000000000000000000000000000000m;
}
string.Format("{0:0.#####}", 0.0030)
or
var money=1.3000m;
money.ToString("0.#####");
For future reference I recommend the .NET Format String Quick Reference by 1 John Sheehan.
decimal value = 0.0030m;
value.ToString(“G29″);
Edit: The G formatter does work, the only 6 problem is that it jumps to scientific notation 5 if there are too many significant figures 4 in the original decimal. Not so ideal.
See 3 the "The General ("G") Format 2 Specifier" documentation here: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#GFormatString
I'm 1 on lunch, so I did a little test:
decimal d1 = 0.000100m;
decimal d2 = 0.001000000000000000000000m;
decimal d3 = 0.000000000000001000000000m;
Console.WriteLine(Environment.NewLine + "input decimal: 0.000100m");
Console.WriteLine("G " + d1.ToString("G"));
Console.WriteLine("G29 " + d1.ToString("G29"));
Console.WriteLine("0.####### " + d1.ToString("0.#######"));
Console.WriteLine(Environment.NewLine + "input decimal: 0.001000000000000000000000m");
Console.WriteLine("G " + d2.ToString("G"));
Console.WriteLine("G29 " + d2.ToString("G29"));
Console.WriteLine("0.####### " + d2.ToString("0.#######"));
Console.WriteLine(Environment.NewLine + "input decimal: 0.000000000000001000000000m");
Console.WriteLine("G " + d3.ToString("G"));
Console.WriteLine("G29 " + d3.ToString("G29"));
Console.WriteLine("0.####### " + d3.ToString("0.#######"));
Output:
input decimal: 0.000100m
G 0.000100
G29 0.0001
0.####### 0.0001
input decimal: 0.001000000000000000000000m
G 0.001000000000000000000000
G29 0.001
0.####### 0.001
input decimal: 0.000000000000001000000000m
G 0.000000000000001000000000
G29 1E-15
0.####### 0
Hmm, this is a display formatting issue 8 (the zeros are added when you convert the 7 decimal to a string).
You need to see where 6 in code you are seeing the trailing zeros. Is 5 it after a call to .ToString()? Try playing 4 around with the different formatting strings:
.ToString("#");
.ToString("0.00");
.ToString("#.##");
And 3 so on. The best way to do this is just 2 to experiment with the different possible 1 values.
decimal m = 0.030000m;
Console.Write(m.ToString("0.##########"));
Just make sure you have enough #s for the 1 number of decimal places you want to display
I use the following. It ensures that any 5 decimal (for which the max precision is 4 29 decimal places) will show all available 3 digits of precision without trailing zeros, and 2 without your code needing to have a long 1 ugly string of hash marks.
if (value is Decimal)
value = ((Decimal)value).ToString("0.".PadRight(29, '#'), culture);
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.