Click here to Skip to main content
15,887,832 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
IS there a way for me to set the textbox to display dollar currency amount? I have the textboxes to populate the numbers from the database. Here is my code:

C#
SqlDataReader dr = scmd.ExecuteReader();
        if (dr.Read())
        {
            TextBoxTA1.Text = dr["TOTASSETS"].ToString();
            TextBoxTL1.Text = dr["TOTLIABILITY"].ToString();
            TextBoxUNA1.Text = dr["UNRNETASSETS"].ToString();
            TextBoxPRNA1.Text = dr["NonExppermresAssets"].ToString();
            TextBoxTR1.Text = dr["TotalRev"].ToString();
            TextBoxTFN1.Text = dr["TuitFees"].ToString();
            TextBoxCD1.Text = dr["CurrDebt"].ToString();
            TextBoxLDT1.Text = dr["LongTermDebt"].ToString();
            

            int a = Convert.ToInt32(TextBoxTA1.Text);
            int b = Convert.ToInt32(TextBoxTL1.Text);
            TextBoxTNA1.Text = Convert.ToString(a - b);

            int a1 = Convert.ToInt32(TextBoxUNA1.Text);
            int b1 = Convert.ToInt32(TextBoxPRNA1.Text);
            TextBoxTNAS1.Text = Convert.ToString(a1 + b1); 



        }
        dr.Close();
        con.Close();
Posted
Comments
[no name] 25-Sep-13 13:26pm    
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
Computer Wiz99 25-Sep-13 13:29pm    
Ok. Thanks. Why didn't you put it in as a solution?

Possibly string.Format("{0:c}", value) would do the trick for you.Check how to use that:
Currency format for display[^]
String Formatting with currency double values not displaying correctly[^]
 
Share this answer
 
ToString can be formatted as in:

C#
TextBox1.Text = floatVar.ToString("#0.00"); //suppose you have a float or double variable
 
Share this answer
 
Comments
Computer Wiz99 25-Sep-13 14:29pm    
Homero Rivera, my code would like like this:

TextBoxTA1.Text = dr["TOTASSETS"] floatVar.ToString("#0.00");?

Is that about right?
Homero Rivera 25-Sep-13 14:36pm    
Try
TextBoxTA1.Text = Double.Parse(dr["TOTASSETS"].ToString()).ToString("#0.00");

Long, I know, but works like a charm for me
Computer Wiz99 25-Sep-13 14:39pm    
Ok. I try and see.
Homero Rivera 25-Sep-13 14:52pm    
Finally, if you really need the dollar sign "$" (just in case) you must concatenate
TextBoxTA1.Text = "$ " + Double.Parse(dr["TOTASSETS"].ToString()).ToString("#0.00");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900