Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in winform text box when user enter 10 and
i store this value in float using float.parse(textbox1.text)
value store as int (10)

but i want to store it in 10.00 formate

after that i do some divison methametic on this value
and suppose answer is 5.239694105
i also want this answer to 5.24



can someone explain me in detail how to do this ?
i am newbe in c#

What I have tried:

using (SqlConnection conn = new SqlConnection(con))
{
using (SqlCommand cmd = new SqlCommand("usp_weightinsertdetails", conn))
{
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("@ProductName", textBox1.Text);
cmd.Parameters.AddWithValue("ProductPieces", int.Parse(textBox2.Text));

float weight, pc, avr;
weight = float.Parse(textBox3.Text);
pc = float.Parse(textBox2.Text);
avr = weight / pc;

cmd.Parameters.AddWithValue("@ProductWeight", weight);
cmd.Parameters.AddWithValue("@AveragePieceWeight", avr);

conn.Open();
cmd.ExecuteNonQuery();
Posted
Updated 1-Jul-16 4:31am

Use a format string to get the output in the required form. See Single.ToString Method (String) (System)[^]. you should also read What Every Computer Scientist Should Know About Floating-Point Arithmetic[^].
 
Share this answer
 
v2
Comments
Member 12592153 1-Jul-16 11:00am    
thanx for solution
Numbers and dates only gain a "format" when you convert them to a string, so the answer to your question is that you can't store your float in a certain format. Keep it as float and when you want to show it convert it to a string of the required format;

C#
float f = 10F;
System.Diagnostics.Debug.WriteLine(f.ToString("0.00"));

f = 1.123F;
System.Diagnostics.Debug.WriteLine(f.ToString("0.00"));
 
Share this answer
 
Comments
Member 12592153 1-Jul-16 11:00am    
and thank you too for helping

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