does money map to float well?

2020-07-26 09:04发布

问题:

does the sql data type money map to c# float?

回答1:

No...it maps to a decimal. If the column allows null, it maps to Nullable<Decimal>.

Mapping CLR Parameter Data

float isn't precise enough to be used for monetary calculations. You'd be losing/gaining money all over the place.



回答2:

I'd use decimal.

The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding.



回答3:

No.

Money maps to Decimal. If the MONEY column allows null values, it will map to Nullable<Decimal>. For details, see SQL-CLR Type Mapping.

Float is not nearly precise enough for numerical computations dealing with money. You should always do all of your calculations using decimal values.



回答4:

No, a float has way too low precision to handle monetary values. Seven digits doesn't get you far. Also a floating point type is prone to rounding errors due to how the numbers are represented.

Use the Decimal data type.