## 6 replies

Last post Jun 09, 2011 04:42 PM by TotalMace

Member

11 Points

58 Posts

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

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"

conversion decimal float

All-Star

24870 Points

4372 Posts

Moderator

MVP

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

You can cast it, like this:

```decimal d = 3.35m;
float f = (float)d;```
`Or you can use the Convert.ToSingle() or Convert.ToDecimal().`

Star

9720 Points

2286 Posts

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

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.

Member

10 Points

74 Posts

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

#### Svante

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.

I agree especially when doing explicit casting.

Star

9720 Points

2286 Posts

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

#### pkw_rob

I agree especially when doing explicit casting.

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.

Member

5 Points

46 Posts

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

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.

Lynn

None

0 Points

1 Post

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

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).