Click here to Skip to main content
15,913,570 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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..
Posted

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:
C#
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:
C#
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:
C#
string s = 123.4;
Which is not going to work. Convert it to a string:
C#
string s = 123.4.ToString();
And it'll work.
So:
C#
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.
 
Share this answer
 
Hi
Try this code...

C#
double tot ;
              double cnbamt;
              txtowndmgamt1000.Text = Math.Round(double.TryParse(txttot1000.Text.Trim(), out tot) - double.TryParse(txtcnbamt1000.Text.Trim(), out cnbamt)).ToString();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900