16

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?

bluish
  • 26,356
  • 27
  • 122
  • 180
Fifty
  • 163
  • 1
  • 1
  • 4

7 Answers7

33

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;
}
Community
  • 1
  • 1
Thomas Materna
  • 3,619
  • 1
  • 17
  • 14
  • 8
    This answer owns because unlike basically every other answer on this question (and even the whole subject) is actually WORKS and does what the OP is asking. Kinda shameful to see all these high-rep users rushing to answer without reading/understanding the question and getting all the upvotes from something that doesn't even work. – Coxy Dec 13 '11 at 01:34
21
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.

Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106
  • 2
    Yes, but what if you might have 6 decimal places, or 7? You end up with a speculative big ugly rash of # characters. – Andrew M Jan 21 '10 at 13:47
  • 2
    This will round the numbers to the nearest 100-thousandth's. This may or may not be what the OP wanted.. – BlueRaja - Danny Pflughoeft Jan 21 '10 at 13:57
  • 3
    @andrew-m Yes the # is a digit placeholder. In most cases you know what the wanted precision is. If you don't and want to be absolutely sure you could use the full precision of the datatype you are currently using. `decimal` can hold 28-29 significant digits. Hacky solution: `.ToString("0.".PadRight(29,'#'))` – Jonas Elfström Jan 21 '10 at 14:47
  • @BlueRaja True and I used five # because he had four decimal places in his examples. – Jonas Elfström Jan 21 '10 at 14:49
  • Thanks Jonas this was a great solution, after trying many things! (it doesnt convert numbers to scientific notation when there are 5 or 6 decimal places) – Jill Oct 24 '13 at 17:01
9
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
Andrew M
  • 9,149
  • 6
  • 44
  • 63
3

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.

Gabriel Magana
  • 4,338
  • 24
  • 23
2
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

Martin Booth
  • 8,485
  • 31
  • 31
0

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);
Alain
  • 26,663
  • 20
  • 114
  • 184
-1
 public static string GentlyRemoveEndZeros(string input)
        {
          //  if (input == null) return null;
          //  if (input == "") return "";
            if (input.Contains(".")) return input.TrimEnd('0').TrimEnd('.');
            return input;
        }
VovaM
  • 342
  • 4
  • 8