473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to convert double to decimal (2 places)

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 49393
Hi,
myTotal = (decimal)(myPrice * myShares);
I doesnt work?

May 21 '06 #2
Never mind - I figured it out.

"Jim Stools" <ji*********@yahoo.com> 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
I builde it in Vs2005,and get the result as:
425189.886

no errors

May 21 '06 #4
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" <ra*******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hi,
myTotal = (decimal)(myPrice * myShares);
I doesnt work?

May 21 '06 #5
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" <ra*******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hi,
myTotal = (decimal)(myPrice * myShares);
I doesnt work?


May 21 '06 #6
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" <gu***@guffa.com> 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" <ra*******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hi,
myTotal = (decimal)(myPrice * myShares);
I doesnt work?


May 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gizmo | last post by:
Hi there i was wondering if any one new to a function that rounds up a float to so many decimal places. I have a number in bytes and converting it to mb's and gb's but once its converted i need to...
3
by: android | last post by:
I require a function that takes a double as a parameter and returns the number of decimal places. What is the most efficient way in c# to find out the number of decimal places I have written a...
1
by: Next | last post by:
hello I am trying to format numbers coming from a datareader using the code below. But it is not working. How do I format decimal places? Thanks in advance! NumberFormatInfo numFormat = new...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
3
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, I would like to convert a double to a string and have the resulting string contain all of the original values after the decimal place. Currently, the problem that I am having is that the...
5
by: =?Utf-8?B?S3VuYWwgUGF0ZWw=?= | last post by:
I have a DataTable with Column DataType as Double I just want to show data in C# datagrid with two decimal places which property do I set for DataColum of DataTable and How? Thanks
3
by: David | last post by:
Any ideas why this statement: mBalanceChange = Double.Parse(strCurrentBalance, Globalization.NumberStyles.AllowThousands Or Globalization.NumberStyles.AllowDecimalPoint) - mBalanceDouble ...
4
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I format a Number as a String with exactly 2 decimal places?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.