454,781 Members | 1,373 Online
Need help? Post your question and get tips & solutions from a community of 454,781 IT Pros & Developers. It's quick & easy.

# How to convert double to decimal (2 places)

 P: n/a How do I do this: result needs to be a decimal to 2 places? I get casting problems on the below: double myPrice = 1200.125;double myShares = 354.288; decimal myTotal; myTotal = myPrice * myShares; Thanks, May 21 '06 #1
6 Replies

 P: n/a Hi, myTotal = (decimal)(myPrice * myShares); I doesnt work? May 21 '06 #2

 P: n/a Never mind - I figured it out. "Jim Stools" wrote in message news:1L******************@tornado.texas.rr.com... How do I do this: result needs to be a decimal to 2 places? I get casting problems on the below: double myPrice = 1200.125;double myShares = 354.288; decimal myTotal; myTotal = myPrice * myShares; Thanks, May 21 '06 #3

 P: n/a I builde it in Vs2005,and get the result as: 425189.886 no errors May 21 '06 #4

 P: n/a My original code snippet would not complie on VS2005, I would get casting errors: where as this works: double myMoney = 1200.125; double myShares = 354.388; decimal myTotal = 0.00m; myTotal = Convert.ToDecimal(myMoney * myShares); You're code also works: myTotal = (decimal)(myPrice * myShares); However in both cases the results are to 4 decimal places instead of 2. Any ideas? "Rader" wrote in message news:11**********************@g10g2000cwb.googlegr oups.com... Hi, myTotal = (decimal)(myPrice * myShares); I doesnt work? May 21 '06 #5

 P: n/a Use the Math.Round method to round the value after the calculation. You should also consider to keep the value without reducing the precision, and round the value when you convert it to a string to display it. (If you do.) Jim Stools wrote: My original code snippet would not complie on VS2005, I would get casting errors: where as this works: double myMoney = 1200.125; double myShares = 354.388; decimal myTotal = 0.00m; myTotal = Convert.ToDecimal(myMoney * myShares); You're code also works: myTotal = (decimal)(myPrice * myShares); However in both cases the results are to 4 decimal places instead of 2. Any ideas? "Rader" wrote in message news:11**********************@g10g2000cwb.googlegr oups.com... Hi, myTotal = (decimal)(myPrice * myShares); I doesnt work? May 21 '06 #6

 P: n/a Thanks...It's for an accounting system so the results need to always stay at 2 decimal places and are stored in a database. "Göran Andersson" wrote in message news:O5**************@TK2MSFTNGP02.phx.gbl... Use the Math.Round method to round the value after the calculation. You should also consider to keep the value without reducing the precision, and round the value when you convert it to a string to display it. (If you do.) Jim Stools wrote: My original code snippet would not complie on VS2005, I would get casting errors: where as this works: double myMoney = 1200.125; double myShares = 354.388; decimal myTotal = 0.00m; myTotal = Convert.ToDecimal(myMoney * myShares); You're code also works: myTotal = (decimal)(myPrice * myShares); However in both cases the results are to 4 decimal places instead of 2. Any ideas? "Rader" wrote in message news:11**********************@g10g2000cwb.googlegr oups.com... Hi, myTotal = (decimal)(myPrice * myShares); I doesnt work? May 22 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.