29

I have the following piece of code:

double shortfall = GetSomeNumber(); //3.3588548831176006E+29

if (shortfall > 0)
{
  returnValue = Convert.ToDecimal(shortfall);
}

That generates the above error.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Burt
  • 7,680
  • 18
  • 71
  • 127
  • I got this error when trying to convert the result of one number divided by 0, which results in infinity so it cannot be converted to decimal. –  Feb 12 '13 at 20:34
  • Just want to add that this error can sometimes show if your data source is configured inproperly (for example you want a string input but somehow it has defaulted to a decimal!) See image below: [![An error that can show if your textbox is mapped to a decimal in Visual Studio](https://i.stack.imgur.com/JEGT9.png)](https://i.stack.imgur.com/JEGT9.png) – Agneum Feb 20 '19 at 11:13

3 Answers3

36

Well, it's fairly self-explanatory.

decimal.MaxValue is 79,228,162,514,264,337,593,543,950,335 - your number is bigger than this.

Although decimal has a finer precision than double, double has a bigger range - it can handle very, very large and very, very small numbers.

Now, if you could tell us what you're really trying to do, we could try to help find a solution... it's rarely a good idea to mix double and decimal, to be honest.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks Jon, I am building a calculation engine and needed access to Math.Pow which only uses doubles. I convert back to decimal for display purposes. Any pointers would be great. – Burt Aug 05 '10 at 10:28
  • 1
    @Burt - if you are simply displaying it, why not display a string representation? – Oded Aug 05 '10 at 10:30
  • 2
    @Burt: What sort of power are you using? It's *relatively* rare to use non-integer powers for `decimal` IME. For integer powers you could just loop and multiply :) – Jon Skeet Aug 05 '10 at 10:36
  • @Jon you are right it is always an integer, but would this error still not occur if I am hitting the upper boundary of the decimal? – Burt Aug 05 '10 at 10:38
  • 3
    @Burt: Yes, you've still fundamentally got a problem here - but I'm suggesting that aside from that, staying within the world of `decimal` would be a good idea (or use `double` to start with). Basically make a choice and stick to it :) – Jon Skeet Aug 05 '10 at 10:43
  • @Jon OK, it is looking like it will have to be double. A couple of questions how best to display doubles is it string.format(...)? What does double map to in SQL server? – Burt Aug 05 '10 at 10:45
  • @Burt: The best way to display a double will depend on the context. Do you wish to show the exact value? Use scientific notation etc? I'm reluctant to comment on the SQL side of things, as it would be so easy to be subtly wrong and it's not an area I have much experience with. – Jon Skeet Aug 05 '10 at 10:58
  • @Jon I have a feeling I should have had your book in with me today. It will be non-scientific notation, and I am assuming I will need string.format for this. – Burt Aug 05 '10 at 11:32
  • @Burt: You don't necessarily need to call `string.Format` - just `x.ToString("some format specifier")` may be simpler. Not that my book would have helped here, I'm afraid. – Jon Skeet Aug 05 '10 at 11:50
15

It means that the value returned cannot be converted to decimal as it is too large.

Decimal values can be between positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335 - see MSDN.

Double can handle much larger numbers - negative 1.79769313486232e308 to positive 1.79769313486232e308. These will not all be convertible to Decimal.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

If you badly need a decimal variable. Then you have to add 1 more condition

if (shortfall > 0)
{
    if (shortfall.ToString().Contains("E"))
        return Convert.ToDecimal($"{shortfall:E8}".Substring(0, 10));
    else if (shortfall.ToString() != "NaN")
        return Convert.ToDecimal(AnnualVal);
    else
        return 0;
}
Wielder
  • 156
  • 2
  • 15