Are you sure you want to do that conversion? If you have the numbers in decimal, that's probably because it's a good idea at some point, or possibly conversely. If you convert to double, you risk loosing precision. If you convert to decimal, you
risk overflow doe to the lesser magnitude possible to represent as decimal.

Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com [Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.

Are you sure you want to do that conversion? If you have the numbers in decimal, that's probably because it's a good idea at some point, or possibly conversely. If you convert to double, you risk loosing precision. If you convert to decimal, you
risk overflow doe to the lesser magnitude possible to represent as decimal.

There are no implicit conversions between float/double and decimal. Implicit numeric conversions are always guaranteed to be without loss of precision or magnitude and will not cause an exception.

Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com [Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.

I realize this posting is very old (3+ years), but nothing listing above solves this problem.

Here is the issue I am running into regarding floating point to decimal conversion that is, I am certain, a similar problem as the original poster ran into. And before someone suggests you shouldn't try to do this because you will lose accuracy, let's remember
we are already using approximately 5 decimal points to the right, we need to use the currency conversion factor as an estimate in a web site, and the value we are trying to value using the currency conversion factor is NOT a floating point number, it is a
decimal/money/currency value -- such as the price of an object.

(1) I am downloading currency conversion values from WebServiceX. These conversion values are returned in floating point (double).

(2) I need to convert these values from floating point to decimal and store the resulting values in a SQL table.

(3) As recommended above, the use of Convert.ToDecimal(), or other similar functionality, does NOT work. The specific conversion statement will execute properly, but the SQL update fails with arithmetic overflow "converting numeric value to numeric value".

There must be some method of dealing with this because until one can resolve this problem, the currency conversion values from WebServiceX are, in effect, utterly useless.

Thanks in advance for picking up this ancient thread!

The Convert.To[X]() method will not safeguard you from conversion problems. It does have a better exception reporting but it will not get you where you want to be.

The problem can be solved in 2 steps:

1st - Solve the conversion in .Net

2nd - Solve the SQL update failure.

SOLVE THE CONVERSION in .NET:

The decimal value is limited to 9 decimal places where the double value does not have such a limit.

So rounding the double at max 9 decimal places solves your issue (also 9 places is enough for all currency values AFAIK)

Use Math.Round(doubleVal, 9) and cast this to a decimal:

## uwillremembe...

Member

11 Points

58 Posts

## Convert "float to decimal" and "decimal to float"

Sep 24, 2007 12:48 AM|uwillrememberme|LINK

Hello,

I'm having a hard time with this conversion...can someone help me out? I'm using C# ASP.NET 1.1

"float to decimal" and "decimal to float"

Thanks so much in advance.

conversion decimal float

## vcsjones

All-Star

24869 Points

4372 Posts

Moderator

MVP

## Re: Convert "float to decimal" and "decimal to float"

Sep 24, 2007 01:41 AM|vcsjones|LINK

You can cast it, like this:

## Svante

Star

9720 Points

2286 Posts

## Re: Convert "float to decimal" and "decimal to float"

Sep 24, 2007 06:12 AM|Svante|LINK

Are you sure you

wantto do that conversion? If you have the numbers in decimal, that's probably because it's a good idea at some point, or possibly conversely. If you convert to double, you risk loosing precision. If you convert to decimal, you risk overflow doe to the lesser magnitude possible to represent as decimal.AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com

[Disclaimer: Code snippets usually uncompiled, beware typos.]

______

Don't forget to click "Mark as Answer" on the post(s) that helped you.

## pkw_rob

Member

10 Points

74 Posts

## Re: Convert "float to decimal" and "decimal to float"

Sep 26, 2007 01:58 PM|pkw_rob|LINK

I agree especially when doing explicit casting.

## Svante

Star

9720 Points

2286 Posts

## Re: Convert "float to decimal" and "decimal to float"

Sep 26, 2007 02:45 PM|Svante|LINK

There

areno implicit conversions between float/double and decimal. Implicit numeric conversions are always guaranteed to be without loss of precision or magnitude and will not cause an exception.AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com

[Disclaimer: Code snippets usually uncompiled, beware typos.]

______

Don't forget to click "Mark as Answer" on the post(s) that helped you.

## LLahman

Member

5 Points

41 Posts

## Re: Convert "float to decimal" and "decimal to float"

May 02, 2011 03:17 PM|LLahman|LINK

I realize this posting is very old (3+ years), but nothing listing above solves this problem.

Here is the issue I am running into regarding floating point to decimal conversion that is, I am certain, a similar problem as the original poster ran into. And before someone suggests you shouldn't try to do this because you will lose accuracy, let's remember we are already using approximately 5 decimal points to the right, we need to use the currency conversion factor as an estimate in a web site, and the value we are trying to value using the currency conversion factor is NOT a floating point number, it is a decimal/money/currency value -- such as the price of an object.

(1) I am downloading currency conversion values from WebServiceX. These conversion values are returned in floating point (double).

(2) I need to convert these values from floating point to decimal and store the resulting values in a SQL table.

(3) As recommended above, the use of Convert.ToDecimal(), or other similar functionality, does NOT work. The specific conversion statement will execute properly, but the SQL update fails with arithmetic overflow "converting numeric value to numeric value".

There must be some method of dealing with this because until one can resolve this problem, the currency conversion values from WebServiceX are, in effect, utterly useless.

Thanks in advance for picking up this ancient thread!

Lynn

## TotalMace

None

0 Points

1 Post

## Re: Convert "float to decimal" and "decimal to float"

Jun 09, 2011 09:42 PM|TotalMace|LINK

The Convert.To[X]() method will not safeguard you from conversion problems. It does have a better exception reporting but it will not get you where you want to be.

The problem can be solved in 2 steps:

1st - Solve the conversion in .Net

2nd - Solve the SQL update failure.

SOLVE THE CONVERSION in .NET:

The decimal value is limited to 9 decimal places where the double value does not have such a limit.

So rounding the double at max 9 decimal places solves your issue (also 9 places is enough for all currency values AFAIK)

Use Math.Round(doubleVal, 9) and cast this to a decimal:

double originalValue = ...

decimal decimalValue = (decimal)Math.Round(originalValue,9);

SOLVE THE SQL ERROR MESSAGE:

The biggest number that a decimal can hold in .Net is 79,228,162,514,264,337,593,543,950,335.

That are 29 characters. But this is a number without any decimal places. So the longest value has 29+9 = 38 characters.

The SQL decimal type is declared the folowing way : Decimal(Number of characters, Number of decimal places)

Using the 38 characters above the declaration of a SQL decimal type that can hold any .Net decimal value should be:

Decimal(38,9).