对于十进制,值太大或太小

时间:2022-10-21 20:21:25

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.

这会产生上述错误。

Can anyone help me with it please?

有人可以帮我吗?

2 个解决方案

#1


23  

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.

decimal.MaxValue是79,228,162,514,264,337,593,543,950,335 - 你的数字比这大。

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

虽然十进制具有比double更精细的精度,但double具有更大的范围 - 它可以处理非常非常大且非常非常小的数字。

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.

现在,如果你能告诉我们你真正想要做什么,我们可以尝试帮助找到解决方案......说实话,混合使用double和decimal几乎不是一个好主意。

#2


7  

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.

十进制值可以介于正数79,228,162,514,264,337,593,543,950,335之间,负数为79,228,162,514,264,337,593,543,950,335 - 见MSDN。

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

Double可以处理更大的数字 - 负数1.79769313486232e308到正数1.79769313486232e308。这些并不都可以转换为Decimal。

#1


23  

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.

decimal.MaxValue是79,228,162,514,264,337,593,543,950,335 - 你的数字比这大。

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

虽然十进制具有比double更精细的精度,但double具有更大的范围 - 它可以处理非常非常大且非常非常小的数字。

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.

现在,如果你能告诉我们你真正想要做什么,我们可以尝试帮助找到解决方案......说实话,混合使用double和decimal几乎不是一个好主意。

#2


7  

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.

十进制值可以介于正数79,228,162,514,264,337,593,543,950,335之间,负数为79,228,162,514,264,337,593,543,950,335 - 见MSDN。

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

Double可以处理更大的数字 - 负数1.79769313486232e308到正数1.79769313486232e308。这些并不都可以转换为Decimal。