Skip navigation

Monthly Archives: April 2008

Sometimes we want to format a double value to monetary value. For example, consider the double value “1234.5678”. We want to format this value as “$1,234.56”. We can do this very easily.

double d = 2234500.2345;
Response.Write("$" + d.ToString("#,#.00"));
//output - $2,234,500.23
Response.Write("$" + d.ToString("#,#.000"));
//output - $2,234,500.235
Response.Write("$" + d.ToString("#,#.0000"));
//output - $2,234,500.2345

It also takes care of rounding the value. consider the following code.

double d1 = 1234.124;
double d2 = 1234.125;
Response.Write("$" + d1.ToString("#,#.00"));
//outputs $1,234.12
Response.Write("$" + d2.ToString("#,#.00"));
//outputs $1,234.13

This technique may be useful in the case when we retrieve data from the database and have to convert a varchar to monetary value. For example, if we are retrieving data using data reader, we can do the following:

//dr is DataReader
Response.Write("$" + (Convert.ToDouble(dr["ColumnName"].ToString())).ToString("#,#.00"));