从百分比中删除小数点并在 .net 中保留百分比符号
remove decimal points from percentage and keep percent symbol in .net
我正在尝试使用下面的代码将十进制转换为这种格式 0.85
到 85%
的字符串,但我总是这样
85.00 %
我正在使用的代码如下所示
item.ModifiedObject.Diversity.ToString("p", CultureInfo.CurrentCulture);
我不确定我哪里做错了,这里我需要删除小数位,同时我需要保留百分号。
任何人都可以提出任何对我来说非常完整的建议。
PS:我不想对 value
使用 split() 函数
先看看文档总是有用的,那就去看看吧
The Percent ("P") Format Specifier
The percent ("P") format specifier multiplies a number by 100 and
converts it to a string that represents a percentage. The precision
specifier indicates the desired number of decimal places. If the
precision specifier is omitted, the default numeric precision supplied
by the current PercentDecimalDigits property is used.
例如
decimal d = 1.23M;
Console.WriteLine(d.ToString("P0"));
Console.WriteLine(d.ToString("P1"));
Console.WriteLine(d.ToString("P2"));
或
decimal d = 1.23M;
Console.WriteLine($"{d:P0}");
Console.WriteLine($"{d:P1}");
Console.WriteLine($"{d:P2}");
输出
123%
123.0%
123.00%
我正在尝试使用下面的代码将十进制转换为这种格式 0.85
到 85%
的字符串,但我总是这样
85.00 %
我正在使用的代码如下所示
item.ModifiedObject.Diversity.ToString("p", CultureInfo.CurrentCulture);
我不确定我哪里做错了,这里我需要删除小数位,同时我需要保留百分号。
任何人都可以提出任何对我来说非常完整的建议。
PS:我不想对 value
使用 split() 函数先看看文档总是有用的,那就去看看吧
The Percent ("P") Format Specifier
The percent ("P") format specifier multiplies a number by 100 and converts it to a string that represents a percentage. The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision supplied by the current PercentDecimalDigits property is used.
例如
decimal d = 1.23M;
Console.WriteLine(d.ToString("P0"));
Console.WriteLine(d.ToString("P1"));
Console.WriteLine(d.ToString("P2"));
或
decimal d = 1.23M;
Console.WriteLine($"{d:P0}");
Console.WriteLine($"{d:P1}");
Console.WriteLine($"{d:P2}");
输出
123%
123.0%
123.00%