显示小数的最佳方式,不需要后跟零

有没有一个显示格式化程序可以输出十进制小数作为这些字符串在 c # 中的表示,而不需要四舍五入?

// 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. # }将舍入,并且使用某些 Trim 类型函数将无法处理网格中的绑定数字列。

116291 次浏览

您是否需要显示最大的小数位数?(你的例子最多只有5个)。

如果是这样的话,我认为使用“0. # # # #”的格式可以满足您的需要。

    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.#####"));
}

扩展方法:

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;
}
}

示例代码:

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()));

产出:

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

我不认为这是可能的开箱即用,但这样一个简单的方法应该做到这一点

public static string TrimDecimal(decimal value)
{
string result = value.ToString(System.Globalization.CultureInfo.InvariantCulture);
if (result.IndexOf('.') == -1)
return result;


return result.TrimEnd('0', '.');
}

开箱即用很容易:

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

将从您的 Decimal 中删除所有尾随零。

您唯一需要做的事情是将 .ToString().TrimEnd('0','.');添加到一个十进制变量中,以便将 Decimal转换为不带尾随零的 String,如上面的示例所示。

在某些区域,它应该是 .ToString().TrimEnd('0',',');(其中使用逗号而不是点,但是您也可以添加一个点和一个逗号作为参数以确保)。

(也可以将两者都作为参数添加)

这种字符串格式应该使您的一天: “0。#############################".记住,小数最多只能有29个有效数字。

例子:

? (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

I just learned how to properly use the G format specifier. See the MSDN 文档. There is a note a little way down that states that trailing zeros will be preserved for decimal types when no precision is specified. Why they would do this I do not know, but specifying the maximum number of digits for our precision should fix that problem. So for formatting decimals, G29 is the best bet.

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

This is yet another variation of what I saw above. In my case I need to preserve all significant digits to the right of the decimal point, meaning drop all zeros after the most significant digit. Just thought it would be nice to share. I cannot vouch for the efficiency of this though, but when try to achieve aesthetics, you are already pretty much damned to inefficiencies.

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;
}

仅此而已,我只是想说说我的看法。

decimal val = 0.000000000100m;
string result = val == 0 ? "0" : val.ToString().TrimEnd('0').TrimEnd('.');

Another solution, based on 阅读障碍 answer, but independent of 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);
}

我最终得到了以下代码:

    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;
}

I have end up with this variant:

public static string Decimal2StringCompact(decimal value, int maxDigits)
{
if (maxDigits < 0) maxDigits = 0;
else if (maxDigits > 28) maxDigits = 28;
return Math.Round(value, maxDigits, MidpointRounding.ToEven).ToString("0.############################", CultureInfo.InvariantCulture);
}

优点:

你可以指定在运行时显示点后的最大有效数字数目;

可以显式指定一个舍入方法;

您可以显式地控制区域性。

可以创建扩展方法

public static class ExtensionMethod {
public static decimal simplify_decimal(this decimal value) => decimal.Parse($"{this:0.############}");
}

如果你愿意接受科学记数法,你可以使用 G0格式字符串:

如果用科学记数法表示数字所产生的指数大于 -5且小于精度说明符,则使用定点表示法,否则使用科学记数法表示法。

可以将此格式字符串用作 .ToString()方法的参数,也可以将其指定为 在一个内插的字符串中。两者如下所示。

decimal hasTrailingZeros = 20.12500m;
Console.WriteLine(hasTrailingZeros.ToString("G0")); // outputs 20.125
Console.WriteLine($"{hasTrailingZeros:G0}"); // outputs 20.125


decimal fourDecimalPlaces = 0.0001m;
Console.WriteLine(fourDecimalPlaces.ToString("G0")); // outputs 0.0001
Console.WriteLine($"{fourDecimalPlaces:G0}"); // outputs 0.0001


decimal fiveDecimalPlaces = 0.00001m;
Console.WriteLine(fiveDecimalPlaces.ToString("G0")); // outputs 1E-05
Console.WriteLine($"{fiveDecimalPlaces:G0}"); // outputs 1E-05