How to convert double to decimal (2 places)

  • Thread starter Thread starter Jim Stools
  • Start date Start date
J

Jim Stools

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,
 
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?
 
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.)
 
Thanks...It's for an accounting system so the results need to always stay at
2 decimal places and are stored in a database.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top