我想转换字符串来执行算术表达式,但获取格式表达式。我想要表达式被计算和最终答案。如何将字符串转换为十进制表达式?
// original value
string text = @"4'-8"x5/16"x20'-8 13/16";
string path = text.Replace("'", "*12").Replace("-", "+").Replace("x", "+").Replace(" ", "+").Replace(@"""", "");
System.Console.WriteLine("The original string: '{0}'", text);
System.Console.WriteLine("The final string: '{0}'", path);
Console.WriteLine();
decimal d = decimal.Parse(path, CultureInfo.InvariantCulture);
Console.WriteLine(d.ToString(CultureInfo.InvariantCulture));
// after converting got this value in debug
//'4*12+8+5/16+20*12+8+13/16'
什么是确切的例外和你在哪一行得到它 – Sybren
使用'decimal.Parse'不会计算存储在字符串中的数学表达式。它不这样工作。你将不得不提取操作数并在你的代码中进行数学运算。 –
十进制d = decimal.Parse(path,CultureInfo.InvariantCulture); –