开发者

Remove 0s from the end of a decimal value [duplicate]

This question already has answers here: Remove trailing zeros (23 answers) Closed 8 years ago. 开发者_StackOverflow中文版

I have a decimal value that has a variable number of digits after the ., for example:

0.0030
0.0310
0.0001
1.1200

How can I write a dynamic function that removes 0 in the end of the decimal?


You can also modify the decimal itself so that any ToString() will give you what you 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 John Sheehan.


decimal value = 0.0030m;
value.ToString(“G29″);

Edit: The G formatter does work, the only problem is that it jumps to scientific notation if there are too many significant figures in the original decimal. Not so ideal.

See the "The General ("G") Format Specifier" documentation here: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#GFormatString

I'm 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 (the zeros are added when you convert the decimal to a string).

You need to see where in code you are seeing the trailing zeros. Is it after a call to .ToString()? Try playing around with the different formatting strings:

.ToString("#");
.ToString("0.00");
.ToString("#.##");

And so on. The best way to do this is just to experiment with the different possible values.


decimal m = 0.030000m;
Console.Write(m.ToString("0.##########"));

Just make sure you have enough #s for the number of decimal places you want to display


I use the following. It ensures that any decimal (for which the max precision is 29 decimal places) will show all available digits of precision without trailing zeros, and without your code needing to have a long ugly string of hash marks.

if (value is Decimal)
   value = ((Decimal)value).ToString("0.".PadRight(29, '#'), culture);


 public static string GentlyRemoveEndZeros(string input)
        {
          //  if (input == null) return null;
          //  if (input == "") return "";
            if (input.Contains(".")) return input.TrimEnd('0').TrimEnd('.');
            return input;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜