Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number formatting in C# 1

Status
Not open for further replies.

meckeard

Programmer
Aug 17, 2001
619
US
Hi,

I am setting a value to a textbox and need to format it like this:

0,000.00

No leading currency sign.

Here is what I have tried with no luck:

txtLoanAmount.Text = (String.Format("{0:c}", parameterLoan_Amount.Value.ToString()));

txtLoanAmount.Text = (String.Format("{0:n}", parameterLoan_Amount.Value.ToString()));

txtLoanAmount.Text = (String.Format("{0:#,###}", parameterLoan_Amount.Value.ToString()));

All of these have NO effect on my value.

Anyone know what I am doing wrong?

Thanks.
 
In VB I would just use:

Code:
TextBox1.Text = Format(1000, "0,000.00")

I don't know C# that well and the only thing I could come up with was:

Code:
string MyValue = string.Format("{0:c}",1000).Replace("£","")

It works but probably isn't the best way of doing it!

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Try this:
Code:
double myVal = 1234.5678;
Label1.Text = myVal.ToString ("c",null);
It will give you the formatting you're looking for.
 
To represent a currency value, you probably should be using the Decimal datatype. It's specifically made for financial operations.

To display it as a formatted currency value, you'd use the System.Globalization.RegionInfo class to retrieve the currency symbol for the region where the currency is used, and the NumberFormat property from System.Globalization.CultureInfo (this returns you a NumberFormatInfo object) to find out how to format your currency value.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks to all those that responded.

Veep's solution was very close. It needed a little modification:

double dblLoanAmount = Convert.ToDouble(parameterLoan_Amount.Value.ToString());

txtLoanAmount.Text = dblLoanAmount.ToString ("n",null);

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top