convert double to decimal without rounding

M

mlafarlett

In the example below, the 'convert.ToDecimal' removes the trailing 5.
I'd
like to get the number, in tact, moved to the decimal variable. Any
help would be greatly appreciated.

Dim dcml As Decimal
Dim dbl As Double = 544.4000244140625
dcml = Convert.ToDecimal(dbl)
Debug.WriteLine(dcml.ToString)

Thx, Michael

P.S. I think getting the double to a string would be a workaround
should the above not be
solved.
 
A

alantolan

System.Convert.ToDecimal( value as double)

converts a double to a decimal. The resulting decimal 'contains 15
significant digits and is rounded using rounding to nearest.'

The 15 significant digits above are 544.400024414062 - no 5.


A decimal can handle up to 28 places of precision so the 'problem' is
with System.Convert.ToDecimal.
Not too sure what the official solution is, perhaps using

System.Convert.ToDecimal(obj as object, fmt as IFormatProvider)

will give you more digits, or (as a stopgap), converting in two stages


- left of the decimal point and right of the decimal point.


hth,
Alan.
 
M

mlafarlett

I gave the 'obj, iFormatProvider' a try but with no luck...its amazing
to me, assuming i'm not doing something stupid, that you can't simply
move a double to a string and it not round it. What makes MS think
everyone wants their number's rounded? I want the whole friggin'
number stored in my string..how bout an "IDontWantItRounded" argument
passed in to the 'convert.todecimal'.....surely there's lots I don't
know in this area. Sorry for the venting...but....
 

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

Top