Hi I have this code in my .cs file and the output is 100449.00 but I want it to format into money like 100,449.00. This is my code to show the value in the label.
billing.Text = "$" + ds.Tables[0].Rows[0]["Billing"].ToString();
billing.Text = "$" + ds.Tables[0].Rows[0]["Billing"].ToString("N");
Per this.
Edit: what's returned is an object which you need to cast to a decimal. Try:
((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("N");
decimal ? does not seem to be...[] operator is an object, therefore a cast to a decimal is needed. Please see my last edit.billing.Text = ds.Tables[0].Rows[0]["Billing"].ToString("c");
["billing"].ToString() with a format specifier as it is a nullable of unknown format typeComing from a DataTable requires us to convert to a non-nullable type before we format as text.
We actually have a few different ways to do the formatting. Your post has a hardcoded dollar sign preceding the value, in which case we can use either the F2 or N2 format strings to give us a decimal point with 2 places to the right and append that to the dollar sign you have in there:
billing.Text = "$" + ((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("F2");
// 123456.7890 will display as $123456.78
billing.Text = "$" + ((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("N2");
// 123456.7890 will display as $123,456.78
Another option is to use the C format which will add in the cultural specific currency symbol and numeric format (decimal points, commas) for us
billing.Text = ((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("C");
// 123456.7890 will display as
// $123,456.78 en-US
// 123 456.78€ fr-FR
// you could also add a second overload to the ToString to specify