如何将decimal
值列表转换为字符串,以便:
例如:
var items = new List<decimal>
{
120.5,
110,
25.356
};
foreach (var item in items)
{
var itemString = item.ToString();
}
这应该会产生以下字符串值:
"120.50"
"110"
"25.356"
答案 0 :(得分:1)
以下扩展方法应满足您的要求。关于代码的评论在OP和评论中提供。
public static string ToFormattedString(this decimal d)
{
//The comma is not mandatory. but
var s = d.ToString();
var tokens = s.Split(new[]{"."}, StringSplitOptions.RemoveEmptyEntries);
//if there are no decimal points 12 then there should no be any zeros and periods (.)
if (tokens.Length == 1)
return s;
//I need to remove trailing zeros
var places = tokens[1].TrimEnd('0');
if (places.Length == 0)
return tokens[0];
//if there is only one decimal point ex- 0.5 then it should be displayed as 0.50
if (places.Length == 1)
return d.ToString("F2");
var format = string.Format("F{0}", places.Length);
return d.ToString(format);
}
像这样使用
var x = new decimal[]{120.5m, 110, 25.356m};
foreach (var item in x)
Console.WriteLine("{0} => {1}", item.ToString(), item.ToFormattedString());
输出:
120.5 => 120.50
110 => 110
25.356 => 25.356
答案 1 :(得分:0)
您可以编写一个方法来获取数字中的小数位数,如下所示:
private static int GetDecimalPlaces(decimal number)
{
return number.ToString().IndexOf('.') == -1 ? 0 :
number.ToString().Substring(number.ToString().IndexOf('.') + 1).Length;
}
然后你可以使用Fx
说明符,其中x是小数位数,如下所示:
value1.ToString("F" + GetDecimalPlaces(value1))
<强>本地化强>
如果您关心本地化,那么请使用下面的代码,因此它适用于任何文化,因为某些文化使用,
作为小数:
private static int GetDecimalPlaces(decimal number)
{
char decimalSeparator =
Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
var index = number.ToString().IndexOf(decimalSeparator);
return index == -1 ? 0 : number.ToString().Substring(index + 1).Length;
}
MSDN上显示了各种格式。这是从那里粘贴的一些代码副本:
decimal value = 16325.62m;
string specifier;
// Use standard numeric format specifiers.
specifier = "G";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: G: 16325.62
specifier = "C";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: C: $16,325.62
specifier = "E04";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: E04: 1.6326E+004
specifier = "F";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: F: 16325.62
specifier = "N";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: N: 16,325.62
specifier = "P";
Console.WriteLine("{0}: {1}", specifier, (value/10000).ToString(specifier));
// Displays: P: 163.26 %
// Use custom numeric format specifiers.
specifier = "0,0.000";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: 0,0.000: 16,325.620
specifier = "#,#.00#;(#,#.00#)";
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));
// Displays: #,#.00#;(#,#.00#): (16,325.62)
答案 2 :(得分:-1)
您可以使用decimal.ToString覆盖来指定格式。
decimal amount = 120.5m;
string str = amount.ToString("0.00");
使用String.Format时也可以使用它。
Console.WriteLine("{0:0.00}", amount);
如果是第一条和第二条规则,则不能在一行上完成。
decimal amount = 120.5m;
string str = amount.ToString("0.00").Replace(".00", String.Empty);