如何在00.00格式中获取文本框浮点值 [英] How to get text box float value in 00.00 formate

查看:117
本文介绍了如何在00.00格式中获取文本框浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户输入10时,在winform文本框中



i使用float.parse(textbox1.text)将此值存储在float中

值存储为int(10)



但是我希望将它存储在10.00格式中



之后我对这个值做了一些divison事件

并且假设答案是5.239694105

i也希望这个答案为5.24







有人可以详细解释一下如何做到这一点吗?

i am newbe in c#



我尝试过:



使用(SqlConnection conn = new SqlConnection(con))

{

使用(SqlCommand cmd = new SqlCommand(usp_weightinsertdetails,conn))

{

cmd.CommandType = CommandType.StoredProcedure;



cmd.Parameters.AddWithValue(@ Date,dateTimePicker1.Value);

c md.Parameters.AddWithValue(@ ProductName,textBox1.Text);

cmd.Parameters.AddWithValue(ProductPieces,int.Parse(textBox2.Text));



浮动重量,个人电脑,avr;

重量= float.Parse(textBox3.Text);

pc = float.Parse( textBox2.Text);

avr =重量/ pc;



cmd.Parameters.AddWithValue(@ ProductWeight,重量);

cmd.Parameters.AddWithValue(@ AveragePieceWeight,avr);



conn.Open();

cmd.ExecuteNonQuery();

解决方案

使用格式字符串以所需的格式获取输出。请参阅 Single.ToString Method(String)(System) [ ^ ]。你还应该阅读每个计算机科学家应该知道的关于浮点运算的知识 [ ^ ]。


当您将数字和日期转换为字符串时,数字和日期只会获得格式,因此您的问题的答案是您不能以特定格式存储浮点数。保持浮动状态,当你想要显示它时将其转换为所需格式的字符串;



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

f = 1 .123F;
System.Diagnostics.Debug.WriteLine(f.ToString( 0.00));


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

解决方案

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[^].


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;

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

f = 1.123F;
System.Diagnostics.Debug.WriteLine(f.ToString("0.00"));


这篇关于如何在00.00格式中获取文本框浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆