Best way to display decimal without trailing zeros

Is there a display formatter that will output decimal numbers as these string representations in C # without rounding?

// decimal -> string 20 -> 20 20.00 -> 20 20.5 -> 20.5 20.5000 -> 20.5 20.125 -> 20.125 20.12500 -> 20.125 0.000 -> 0 

{0. #} will round, and using some function like Trim will not work with the associated number column in the grid.

+74
decimal c # formatting
Jun 23 2018-10-18T00-06-23
source share
10 answers

Do you have the maximum number of decimal places you will ever need to display? (Your examples have a maximum of 5).

If so, I think formatting with "0. #####" will do what you want.

  static void Main(string[] args) { var dList = new decimal[] { 20, 20.00m, 20.5m, 20.5000m, 20.125m, 20.12500m, 0.000m }; foreach (var d in dList) Console.WriteLine(d.ToString("0.#####")); } 
+107
Jun 23 '10 at 18:59
source share

I just found out how to use the G format specifier correctly. See the MSDN documentation . There is a small note indicating that trailing zeros will be preserved for decimal types if precision is not specified. Why they do this, I do not know, but specifying the maximum number of digits for our accuracy should fix this problem. Thus, for formatting decimal places, the best option is G29 .

 decimal test = 20.5000m; test.ToString("G"); // outputs 20.5000 like the documentation says it should test.ToString("G29"); // outputs 20.5 which is exactly what we want 
+17
Dec 30 2018-11-11T00:
source share

This line format should make your day: "0. ###############################". Keep in mind that decimals can have no more than 29 significant digits.

Examples:

 ? (1000000.00000000000050000000000m).ToString("0.#############################") -> 1000000.0000000000005 ? (1000000.00000000000050000000001m).ToString("0.#############################") -> 1000000.0000000000005 ? (1000000.0000000000005000000001m).ToString("0.#############################") -> 1000000.0000000000005000000001 ? (9223372036854775807.0000000001m).ToString("0.#############################") -> 9223372036854775807 ? (9223372036854775807.000000001m).ToString("0.#############################") -> 9223372036854775807.000000001 
+13
Sep 10 2018-11-11T00:
source share

This is another variation of what I saw above. In my case, I need to save all the significant digits to the right of the decimal point, which means the fall of all zeros after the most significant digit. Just thought it would be nice to share. I cannot vouch for the effectiveness of this, though, but when you try to achieve aesthetics, you are already largely cursed by inefficiency.

 public static string ToTrimmedString(this decimal target) { string strValue = target.ToString(); //Get the stock string //If there is a decimal point present if (strValue.Contains(".")) { //Remove all trailing zeros strValue = strValue.TrimEnd('0'); //If all we are left with is a decimal point if (strValue.EndsWith(".")) //then remove it strValue = strValue.TrimEnd('.'); } return strValue; } 

That's all, just wanted to drop my two cents.

+7
Oct 18 '12 at 19:22
source share

Extension Method:

 public static class Extensions { public static string TrimDouble(this string temp) { var value = temp.IndexOf('.') == -1 ? temp : temp.TrimEnd('.', '0'); return value == string.Empty ? "0" : value; } } 

Code example:

 double[] dvalues = {20, 20.00, 20.5, 20.5000, 20.125, 20.125000, 0.000}; foreach (var value in dvalues) Console.WriteLine(string.Format("{0} --> {1}", value, value.ToString().TrimDouble())); Console.WriteLine("=================="); string[] svalues = {"20", "20.00", "20.5", "20.5000", "20.125", "20.125000", "0.000"}; foreach (var value in svalues) Console.WriteLine(string.Format("{0} --> {1}", value, value.TrimDouble())); 

Output:

 20 --> 20 20 --> 20 20,5 --> 20,5 20,5 --> 20,5 20,125 --> 20,125 20,125 --> 20,125 0 --> 0 ================== 20 --> 20 20.00 --> 2 20.5 --> 20.5 20.5000 --> 20.5 20.125 --> 20.125 20.125000 --> 20.125 0.000 --> 0 
+4
Jun 23 '10 at 19:12
source share

This is pretty easy to do out of the box:

 Decimal YourValue; //just as example String YourString = YourValue.ToString().TrimEnd('0','.'); 

which will remove all trailing zeros from the decimal number.

The only thing you need to do is add .ToString().TrimEnd('0','.'); into a decimal variable to convert the decimal code to a string without trailing zeros, as in the example above.

In some regions, this should be .ToString().TrimEnd('0',','); (where they are a comma instead of us, but you can also add a period and a comma as parameters)

(you can also add both parameters)

+3
Jun 23 '10 at 19:24
source share

Another solution based on dyslexicanaboko answer, but not depending on the current culture:

 public static string ToTrimmedString(this decimal num) { string str = num.ToString(); string decimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; if (str.Contains(decimalSeparator)) { str = str.TrimEnd('0'); if(str.EndsWith(decimalSeparator)) { str = str.RemoveFromEnd(1); } } return str; } public static string RemoveFromEnd(this string str, int characterCount) { return str.Remove(str.Length - characterCount, characterCount); } 
+2
Jul 18 '16 at 14:52
source share

I don't think this is possible out of the box, but an easy way like this is to do it

 public static string TrimDecimal(decimal value) { string result = value.ToString(System.Globalization.CultureInfo.InvariantCulture); if (result.IndexOf('.') == -1) return result; return result.TrimEnd('0', '.'); } 
+1
Jun 23 '10 at 19:14
source share
 decimal val = 0.000000000100m; string result = val == 0 ? "0" : val.ToString().TrimEnd('0').TrimEnd('.'); 
+1
May 26 '15 at 7:37
source share

I ended up with the following code:

  public static string DropTrailingZeros(string test) { if (test.Contains(CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator)) { test = test.TrimEnd('0'); } if (test.EndsWith(CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator)) { test = test.Substring(0, test.Length - CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator.Length); } return test; } 
0
Jun 09 '17 at 9:50
source share



All Articles