views:

194

answers:

5

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

+2  A: 

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.

JonH
+10  A: 

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.

Justin Niessner
Only if the column allows nulls - otherwise, it maps to Decimal directly.
Reed Copsey
@Reed - Thanks for the clarification. Got ahead of myself. Updated appropriately.
Justin Niessner
Why does it not show that the answer was edited?
CSharpAtl
@CSharpAtl - I wish I knew. Maybe something to post over in meta. It's actually been updated twice now and I'm still not showing any edits.
Justin Niessner
Answers don't show edits made within the first 5 minutes. One of the annoying things with the site that is by-design.
R. Bemrose
There's a timeout - if you edit your answer within a short window (I think it's 30 seconds), the system merges the changes together. IIRC, this is so that you can make minor edits for spelling and grammar without adding revisions that need to be tracked.
Bevan
@Justin: Decimal? Guess you haven't see Superman 3 ;)
OMG Ponies
@OMG Ponies - Indeed, I have. I prefer a good Office Space reference though.
Justin Niessner
A: 

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.

Reed Copsey
A: 

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.

Guffa
+2  A: 

Only if you want to lose money.

eyelidlessness
+1 for snark, -1 for no alternative.
Will