You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何避免decimal/double存储数值时精度丢失?含double对象复制场景

Why Precision Loss Happens with Double/Decimal and How to Fix It

Hey there! Let's walk through why you're seeing that unexpected rounding when converting values to Decimal, and how to handle your specific scenario where you're pulling values from a Double-storing object.

First: The Core Difference Between Double and Decimal

To understand the precision loss, you need to know how these two types store numbers:

  • Double is a binary floating-point type. It represents numbers using binary fractions, but many decimal values (like your 210.00483839999998) can't be represented exactly with a finite number of binary digits. So when you store such a value as a Double, it's already an approximation of the original decimal number.
  • Decimal is a decimal floating-point type. It's designed specifically to represent decimal numbers exactly (up to its precision limit of 28-29 significant digits). The problem here is that you're starting with a Double, which already holds an approximate value—converting that to Decimal can't recover the original precise number.

Why Your Specific Example Rounds to 210.0048384

When you write:

Dim someDecimalVar as Decimal = 210.00483839999998

The compiler first interprets 210.00483839999998 as a Double literal (since there's no D suffix to mark it as Decimal). That Double can't hold the exact decimal value, so it stores the closest possible binary approximation. When you convert that approximate Double to Decimal, the result is 210.0048384—the closest Decimal representation of that binary approximation.

How to Fix This (Especially When Pulling from a Double Object)

Your scenario is trickier because you're getting values from an object that stores Doubles. Here are your options:

1. Fix the Source (If Possible)

If you have control over the object storing the values, stop using Double for precise decimal data. Switch it to store Decimal instead. This eliminates the approximation at the source, so you can directly copy the Decimal values without losing precision.

2. Use String Representation as an Intermediate

If you can't change the object's type, check if you can get the original value as a String instead of a Double. Strings preserve exact decimal digits, so you can parse the String directly to Decimal:

' If you can get the original value as a string
Dim originalValueStr As String = "210.00483839999998"
Dim preciseDecimal As Decimal = Decimal.Parse(originalValueStr)

This will give you the exact value you need, since you're bypassing the Double approximation entirely.

3. The Hard Truth: If You Only Have the Double, You Can't Recover the Exact Value

If you can't get anything other than the Double value, it's impossible to retrieve the original precise decimal number. The Double already holds an approximation—there's no way to distinguish your original 210.00483839999998 from other decimal numbers that round to the same Double value. In this case, you'll have to accept the precision loss, or adjust your data pipeline to avoid storing precise decimal values as Doubles in the first place.

Quick Tip for Direct Decimal Assignments

For future reference, if you're assigning a decimal literal directly to a Decimal variable, add the D suffix to tell the compiler to treat it as a Decimal from the start, avoiding Double conversion:

Dim someDecimalVar As Decimal = 210.00483839999998D

This will store the exact value you wrote, no rounding involved.


内容的提问来源于stack exchange,提问作者Detlef D Soost

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:34:03