转换问题sting到int [英] Conversion problem sting to int
问题描述
嗨朋友们,
我是C#的新手,
我正在将vb.net项目转换为C#
txtowndmgamt1000.Text = Math.Round(Convert.ToDouble(txttot1000.Text) - Convert.ToDouble(txtcnbamt1000.Text)
);
无法隐式转换类型'双'到'字符串'
请帮助我....
提前致谢..
并且错误消息非常明确:无法将类型'double'隐式转换为'string'
Convert.ToDouble
将字符串转换为double,因此您可以有效地排队:
double d1 = Convert.ToDouble(txttot1000.Text);
double d2 = Convert.ToDouble(txtcnbamt1000.Text);
txtowndmgamt1000.Text = Math.Round(d1 - d2);
Math.Round
需要double,并返回一个double。所以你的代码变成:
double d1 = Convert.ToDouble(txttot1000.Text);
double d2 = Convert.ToDouble(txtcnbamt1000.Text);
double d3 = Math.Round(d1 - d2);
txtowndmgamt1000.Text = d3;
Text属性是一个字符串。所以你要做的是:
string s = 123 。 4 ;哪个不起作用。将其转换为字符串:
string s = 123 。 4 .ToString();它会起作用。
所以:
txtowndmgamt1000.Text = Math.Round(Convert.ToDouble(txttot1000.Text) - Convert.ToDouble(txtcnbamt1000.Text))。ToString();你需要的是什么。
但是我不这样做:如果用户在你的一个文本框中输入Hello而不是123.4 ,你的程序会崩溃。使用 Double.TryParse [ ^ ],并向用户报告问题。
你好b $ b
试试这个代码...
double tot;
double cnbamt;
txtowndmgamt1000.Text = Math.Round( double .TryParse(txttot1000.Text.Trim(), out tot) - double .TryParse(txtcnbamt1000.Text.Trim(), out cnbamt))的ToString();
Hi friends,
I am new in C#,
I am converting vb.net project into C#
txtowndmgamt1000.Text = Math.Round(Convert.ToDouble(txttot1000.Text) - Convert.ToDouble(txtcnbamt1000.Text)
);
Cannot implicitly convert type 'double' to 'string'
Please Help me....
Thanks in advance..
And the error message is pretty explicit: "Cannot implicitly convert type 'double' to 'string'"
Convert.ToDouble
converts a string to a double, so you line is effectively:
double d1 = Convert.ToDouble(txttot1000.Text); double d2 = Convert.ToDouble(txtcnbamt1000.Text); txtowndmgamt1000.Text = Math.Round(d1 - d2);
Math.Round
takes a double, and returns a double. So your code becomes:
double d1 = Convert.ToDouble(txttot1000.Text); double d2 = Convert.ToDouble(txtcnbamt1000.Text); double d3 = Math.Round(d1 - d2); txtowndmgamt1000.Text = d3;
And the Text property is a string. So what you are trying to do is:
string s = 123.4;Which is not going to work. Convert it to a string:
string s = 123.4.ToString();And it'll work.
So:
txtowndmgamt1000.Text = Math.Round(Convert.ToDouble(txttot1000.Text) - Convert.ToDouble(txtcnbamt1000.Text)).ToString();Is what you need.
But I wouldn't do it that way: if the user enters "Hello" instead ot "123.4" in one of your textboxes, your program will crash. Use Double.TryParse[^] instead, and report problems to the user.
Hi
Try this code...
double tot ; double cnbamt; txtowndmgamt1000.Text = Math.Round(double.TryParse(txttot1000.Text.Trim(), out tot) - double.TryParse(txtcnbamt1000.Text.Trim(), out cnbamt)).ToString();
这篇关于转换问题sting到int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!