0

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();

3 Answers 3

2
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");
Sign up to request clarification or add additional context in comments.

5 Comments

I get this error: No overload for method 'ToString' takes '1' arguments
@ParbhuBissessar are you sure that your value is in decimal ? does not seem to be...
I'm sure that it is.
@ParbhuBissessar that makes one wonder, because the documentation states clearly that this overload exist.
What's returned by the [] operator is an object, therefore a cast to a decimal is needed. Please see my last edit.
1
billing.Text = ds.Tables[0].Rows[0]["Billing"].ToString("c");

4 Comments

OP asked to print out something like 100,449.00, this prints $100,449.00.
I get this error: No overload for method 'ToString' takes '1' arguments
@ytoledano i gave the op what they wanted, not what they asked for.
@ytoledano The OP had the dollar sign hard coded at the beginning of the line. However, you cannot go from an object ["billing"].ToString() with a format specifier as it is a nullable of unknown format type
0

Coming 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.