C#,表达式(string)转换成Double

WIKESOFT
WIKESOFT 官方账号
2010-08-10 10:14:49
static void Main(string[] args)
{
double ss = (1000 + 200) / 1000 * 2;
Console.WriteLine(ss);


string fomula = "(1000 + 200) / 1000 * 2";
double dd = Convert.ToDouble(fomula);

Console.WriteLine(dd);

Console.ReadLine();
}
这段代码要报异常,输入的字符串的格式不正确
double dd = Convert.ToDouble(fomula);
请问Csdn上的友友们,string fomula = "(1000 + 200) / 1000 * 2"; 怎么计算它的结果呢


...全文
1630 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Peter200694013 2010-08-10
  • 打赏
  • 举报
回复

string fomula = "(1000 + 200) / 1000 * 2";

Object o = new DataTable().Compute(fomula,"");

double d;
if (Double.TryParse(o.ToString(), out d))
Console.WriteLine(d);
wubbzz 2010-08-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 lstcyzj 的回复:]
static void Main(string[] args)
{
double ss = (1000 + 200) / 1000 * 2;
Console.WriteLine(ss);


string fomula = "(1000 + 200) / 1000 * 2";
double dd = Convert.ToDouble(fomula);

……
[/Quote]

这种应该使用js中的eval。
C#中有接口,注意找找看。
wuyq11 2010-08-10
  • 打赏
  • 举报
回复
object o=new DataTable().Compute("(1000 + 200) / 1000 * 2","");
Convert.TryDouble
兔子-顾问 2010-08-10
  • 打赏
  • 举报
回复
double ss = (1000 + 200) / 1000 * 2;
不知道他想的是不是就要整数的。如果要数值上正确。应该是
double ss = (1000.0 + 200.0) / 1000.0 * 2;
chenchenyangll 2010-08-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyazhe 的回复:]

C# code

static void Main(string[] args)
{
double ss = (1000 + 200) / 1000 * 2;
Console.WriteLine(ss);


string fomula = "(1000 + 200) / 1000 * 2";
double dd = (double)new System.Dat……
[/Quote]


学习了 接分
xy325432 2010-08-10
  • 打赏
  • 举报
回复

double ss = (1000 + 200) / 1000 * 2;
Console.WriteLine(ss);

ss的值不对?
c2008200613 2010-08-10
  • 打赏
  • 举报
回复
注意类型的转换啊啊
Icefile 2010-08-10
  • 打赏
  • 举报
回复
为什么一定要定义为string类型呢
兔子-顾问 2010-08-10
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
double ss = (1000 + 200) / 1000 * 2;
Console.WriteLine(ss);


string fomula = "(1000 + 200) / 1000 * 2";
double dd = (double)new System.Data.DataTable().Compute(fomula, "");

Console.WriteLine(dd);

Console.ReadLine();
}
Purple_Sky2010 2010-08-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyazhe 的回复:]

C# code

static void Main(string[] args)
{
double ss = (1000 + 200) / 1000 * 2;
Console.WriteLine(ss);


string fomula = "(1000 + 200) / 1000 * 2";
double dd = (double)new System.Dat……
[/Quote]


楼上说的没错,楼主试试哈
Alfredknox 2010-08-10
  • 打赏
  • 举报
回复
就像string str="0.1";convert.toint32(str);一样报错,这个式子应该先把str转为double类型再转才可以即:convert.toint32(convert.todouble(str));
Alfredknox 2010-08-10
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
double ss = (1000 + 200) / 1000 * 2;
Console.WriteLine(ss);


string fomula = "(1000 + 200) / 1000 * 2";
double dd = (double)new DataTable().Compute(fomula, "");

Console.WriteLine(dd);

Console.ReadLine();
}

LZ这样写:string fomula = "(1000 + 200) / 1000 * 2";
double dd = Convert.ToDouble(fomula);肯定是错误的,它会把(1000 + 200) / 1000 * 2当作double类型去处理了,所以肯定会报错
gohappy2008 2010-08-10
  • 打赏
  • 举报
回复


Double dd=Convert.ToDouble(new DataTable().Compute("(1000 + 200) / 1000 * 2",""));
xihuanxiatian 2010-08-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 lstcyzj 的回复:]
static void Main(string[] args)
{
double ss = (1000 + 200) / 1000 * 2;
Console.WriteLine(ss);


string fomula = "(1000 + 200) / 1000 * 2";
……
[/Quote]
string fomula = "(1000 + 200) / 1000 * 2";
double dd = Convert.ToDouble(fomula);

这个string中有这么多运算符 怎么转换成double类型

汗。。。

110,574

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧