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:

Post a Comment

How to Auto-generate Order Line Item numbers for bulk uploads

 I had a problem where I had 17000 line items to insert into 9000 orders. The system required line item numbers, preferably numbered 1throug...