Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here in the below code EstbFee=0.00 and FutureEstbFee=4000.10 but when im trying to add these two it's resulting in "04000.1” rather it’s sum should display 4000.10 . How can solve it

C#
txtEstablishmentFee.Text = (Convert.ToDouble(dt.Rows[0]["EstablishmentFee"]) + Convert.ToDouble(dt.Rows[0]["FutureEstablishmentFee"])).ToString().Trim();



Thanks in advance
Posted
Updated 30-Nov-15 21:50pm
v3

It's because you are actually concatenating two strings. Please note that you call ToString() on the second value. Try this:
C#
txtEstbFee.Text = (Convert.ToDouble(dt.Rows[0]["EstbFee"]) + Convert.ToDouble(dt.Rows[0]["FutureEstbFee"])).ToString().Trim();

Or better break into several steps:
C#
var estbFee = Convert.ToDouble(dt.Rows[0]["EstbFee"]);
var futureEstbFee = Convert.ToDouble(dt.Rows[0]["FutureEstbFee"]);
var totalFee = estbFee + futureEstbFee; 
txtEstbFee.Text = totalFee.ToString();

This way it's easy to read.
 
Share this answer
 
Comments
Praveen_P 1-Dec-15 4:31am    
+ 5
Stop playing with strings!
Try:
C#
txtEstbFee.Text = (Convert.ToDouble(dt.Rows[0]["EstbFee"]) + Convert.ToDouble(dt.Rows[0]["FutureEstbFee"])).ToString();
Or better:
C#
txtEstbFee.Text = ((double)dt.Rows[0]["EstbFee"] + (double)dt.Rows[0]["FutureEstbFee"]).ToString();
Assuming your columns are numeric. If they aren't, they should be!
 
Share this answer
 
You convert the second value to double and back to a string again. You need to surround it with braces or split up the line like below. Just replace value with a more meaningful name :)
C#
double value = Convert.ToDouble(dt.Rows[0]["EstbFee"]) + Convert.ToDouble(dt.Rows[0]["FutureEstbFee"]);
txtEstbFee.Text = value.ToString().Trim();


Good luck!

ps. why are the columns string instead of beeing a decimal type? Then no conversion is needed.
 
Share this answer
 
v2
Comments
JanardhanSharma 1-Dec-15 3:52am    
Oops! I misplaced the brace and din't even notice it..

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