Convert.ToInt32()根据带逗号的字符串进行条件格式化

时间:2011-12-06 15:09:02

标签: c#

string str = e.Row.Cells[index].Text;

int value = Int32.Parse(str, NumberStyles.AllowThousands, NumberFormatInfo.InvariantInfo);
if (value >= 100)
    e.Row.Cells[index].BackColor = System.Drawing.Color.Green;

细胞值为168,88 - 125,45 - 75,3

解析str返回16888 - 12545 - 753后,所有单元格都设置为绿色

我如何比较实际价值。?

2 个答案:

答案 0 :(得分:4)

您正在使用NumberFormatInfo.InvariantInfo。这会将,视为千位分隔符。

你确定这是正确的吗?您的意思是使用CultureInfo.GetCulture("fr-FR")之类的内容,其中,是小数点分隔符吗?

此外,如果您需要保留小数部分,为什么要解析为整数?

这应该对你更好:

decimal.Parse(str, NumberStyles.AllowThousands, CultureInfo.GetCulture("fr-FR"));

答案 1 :(得分:0)

我认为您正在寻找的是:

int value = Int32.Parse(str, NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo);

NumberFormatInfo告诉Parse函数应该如何解释输入。 InvariantInfo从msdn。读取为Gets the default read-only NumberFormatInfo that is culture-independent (invariant)