Pages

Tuesday, February 22, 2011

SQL Server Money Type Conversion

How to convert a SQL Server money Type to a C# floating point number.
The easiest way yo do this is to use the C# decimal type.

decimal amount = (decimal)myReader["Amount"];

I had tried double and float, and they both gave me an explicit conversion error.

FYI: when initializing a decimal, use this:

decimal mynumber=0m;

Think of the "m" as "money".

In format strings, use {0:C} (currency format) to display.

string.format("{0:C}",amount);


Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

No comments:

Share This!

Contact Us

Name

Email *

Message *