Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value was either too large or too small for a Decimal

Tags:

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.

like image 945
Burt Avatar asked Aug 05 '10 10:08

Burt


1 Answers

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.

like image 184
Jon Skeet Avatar answered Oct 14 '22 03:10

Jon Skeet