Reputation: 2078
I am attempting to set an asp.net textbox to a SQL 2005 money data type field, the initial result displayed to the user is 40.0000 instead of 40.00. In my asp.net textbox control I would like to only display the first 2 numbers after the decimal point e.g. 40.00
What would be the best way to do this? My code is below:
this.txtPayment.Text = dr["Payment"].ToString();
Upvotes: 2
Views: 3050
Reputation: 16540
this.txtPayment.Text = string.Format("{0:c}", dr[Payment"].ToString());
Upvotes: 2
Reputation: 2078
After some research I came up with the following:
string pmt = dr["Payment"].ToString();
double dblPmt = System.Convert.ToDouble(pmt);
this.txtPayment.Text = dblPmt.ToString("c",CultureInfo.CurrentCulture.NumberFormat);
I am going to test all code samples given. If I can solve this with one line of code then thats what I am going to do.
Upvotes: 0
Reputation: 95462
@Matt Hamilton
It does. "c" works for whatever the CurrentCultureInfo is, the question becomes if all the users of the web application have the same currency as the server, otherwise, they will need to get the cultureinfo clientside and use the currency gleaned from there.
Upvotes: 0
Reputation: 204229
Does the "c" format string work on ASP.NET the same way as it does in, say, Windows Forms? Because in WinForms I'm fairly certain it obeys the client's currency settings. So even if the value is stored in US Dollars, if the client PC is set up to display Yen then that's the currency symbol that'll be displayed. That may not be what you want.
It may be wiser if that's the case to use:
txtPayment.Text = dr["Payment"].ToString("00.00")
Upvotes: 2
Reputation: 7521
Use the ToString method with "c" to format it as currency.
this.txtPayment.Text = dr["Payment"].ToString("c");
Standard Numeric Format Strings
Upvotes: 0