How to format a C # decimal to remove the extra following 0?

I want to format the string as decimal, but the decimal number contains the following zeros after the decimal. How to format it so that these meaningless 0s disappear?

string.Format("{0}", 1100M); string.Format("{0}", 1100.1M); string.Format("{0}", 1100.100M); string.Format("{0}", 1100.1000M); 

displayed:

 1100 1100.1 1100.100 1100.1000 

but I want it to be:

 1100 1100.1 1100.1 1100.1 

For reference, here are other questions that are essentially duplicates of this, which I found thanks to the answers given here:

  • Parse the decimal character and add filter 0 to the right?
  • Best way to display decimal without trailing zeros
  • How to disable zeros and decimal points of decimal strings?
+42
tostring decimal c # format
Jan 24 '11 at 20:18
source share
11 answers

You can use ToString() with a format specifier () to achieve the desired result. Ending zeros are truncated when using this format string with the specified precision. To prevent rounding in any situation, you need to set the accuracy to the maximum allowed for decimal places (29).

The line of code to create what you want is number.ToString("G29") , where number is your original decimal place.

Keep in mind that any numbers less than 0.0001 will be converted to scientific notation. More information on how this formatter works can be found in the link above.

+77
Jan 24 '11 at 21:20
source share
 string s = d.ToString("0.#############################"); 
+20
Apr 6 2018-11-11T00:
source share

They are not necessarily meaningless - they indicate accuracy during the calculation. Decimal numbers retain their level of accuracy, rather than normalizing.

I have code in this answer that will return a normalized value - you can use it and then format the result. For example:

 using System; using System.Numerics; class Test { static void Display(decimal d) { d = d.Normalize(); // Using extension method from other post Console.WriteLine(d); } static void Main(string[] args) { Display(123.4567890000m); // Prints 123.456789 Display(123.100m); // Prints 123.1 Display(123.000m); // Prints 123 Display(123.4567891234m); // Prints 123.4567891234 } } 

I suspect that most substrings of format string will not work. I would suggest that the format string is "0." and then 28 # characters would work, but that would be very ugly ...

+4
Jan 24 '11 at 20:22
source share

You can specify the format string as follows:

 String.Format("{0:0.000}", x); 
+3
Jan 24 '11 at 20:21
source share

What about:

 string FormatDecimal(decimal d) { const char point = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator[0]; string s = d.ToString(); // if there no decimal point, there nothing to trim if (!s.Contains(point) == -1) return s; // trim any trailing 0s, followed by the decimal point if necessary return s.TrimEnd('0').TrimEnd(point); } 
+3
Jan 24 2018-11-21T00:
source share

Unlike everyone suggesting using the G format specifier, I would suggest the following to keep both a thousand separators and a decimal point when deleting extra zeros:

 {0:#,#.##} 

The result of this format is in most cases much better than G:

 String.Format("{0:#,#.##}",25/2.4); 10.42 String.Format("{0:#,#.##}",1000000); 1,000,000 String.Format("{0:#,#.##}",1000000.3600000); 1,000,000.36 

And the G specifier cannot handle all possible combinations:

 String.Format("{0:G29}",25/2.4); 10.416666666666668 String.Format("{0:G2}",25/2.4); 10 String.Format("{0:G29}",1000000.3600000); 1000000.36 String.Format("{0:G2}",1000000.3600000); 1E+06 
+3
Apr 30 '15 at 7:16
source share

Already a few answers. I often talk about this cheat sheet: http://blog.stevex.net/string-formatting-in-csharp/

+2
Jan 24 '11 at 20:25
source share
 String.Format("{0:0.##}", 123.0); // "123" 
+1
Jan 24 '11 at 20:21
source share

I believe what you want to do:

 var s = String.Format("{0:#####.###}"); 
+1
Jan 24 '11 at 20:23
source share

A bit hacky, but this should work:

 decimal a = 100.00M; string strNumber = string.Format("{0}", a); Console.WriteLine(strNumber.Contains('.') ? strNumber.TrimEnd('0').TrimEnd('.') : strNumber); 
+1
Jan 24 '11 at 20:29
source share
 double a = 1100.00 double b =1100.1 double c = 1100.100 double d =1100.1000 Remove last zero after point string stra = a.toString("0.00").TrimEnd('0').TrimEnd('.'); string strb = b.toString("0.00").TrimEnd('0').TrimEnd('.'); string strc = c.toString("0.00").TrimEnd('0').TrimEnd('.'); string strd = d.toString("0.00").TrimEnd('0').TrimEnd('.'); Output 1100 1100.1 1100.1 1100.1 
-2
Dec 11 '13 at 14:16
source share



All Articles