In classic ASP we had FormatCurrency to take a value (number) and display it as a formatted currency.
In C#, how do we achieve the same?
To map a SQL 205 Money datatype to a C# class property, I use Decimal Data Type – which gives you the precision you need.
A simple .ToString() on this, would display just that – the decimal, as a string.
But what if you want to display a price??
Well, you can use String.Format
See below for an example:
var priceString = String.Format("Price: {0:C}", priceValue.ToString());
Now, in our example above, we use priceValue – this is the decimal value, with a .ToString() on the end (not entirely sure if this is required)
This will display:
Price: £20.38
(assuming the value of priceValue was 20.38)
This is dependant on the set up of the server / computer the application is running on… for example, if it is set up for USA, it will display $ instead of £.
Leave a Reply