如何在窗口应用程序中设置文本框值..... [英] how to formate textbox value .....in window application

查看:69
本文介绍了如何在窗口应用程序中设置文本框值.....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友.....

我有一个文本框,我要输入50或60,并且要在离开文本框时显示50.00之类的结果..任何伙伴都可以帮我做什么...


感谢高级

Lakhan

hello friend .....

i have textbox in which i enter like 50 or 60 and i want to show result like 50.00 as i leave textbox ....can any buddy help me what i have to do...


thanks in advanced

Lakhan

推荐答案

string  valueString = string.Format("{0:#.00}", value);


检查此MSDN链接
LostFocus事件 [标准数字格式字符串 [
Check this MSDN links
LostFocus Event[^]
Standard Numeric Format Strings[^]


您好,
如果您在文本框中输入一个属于实数集的数字,
您应该知道文本框的值仍然是
简单的文本或字符串.输入的数字作为字符串应转换为实数.
因此,您首先需要知道用户是否输入了号码.
为此,我通常使用 TryParse 方法,此外,如果用户具有
输入的数字,如果超过则应四舍五入
小数点后两位 Math.Round(Number,2)
最后是在文本框中显示它
以纯文本格式或以以下格式"60"表示的字符串-> "60.00",
通过使用 Number.ToString("### ## 0.00")巫婆将实数转换为具有所示字符串格式的简单字符串,并将其舍入为两位小数:


Hello,
If you enter into textbox a number that belongs to the set of real numbers,
you should know that the value of the text box is still
simple text or string.Entered number as a string shuld be transfered to real number.
So you first need to know whether the user entered number.
For this I usually use TryParse method, afther that if user have
entered the number you should round it if it have more than
two decimal places Math.Round(Number,2),
and the last is to show it in the text box
as a plain text or a string in following format "60" -> "60.00",
by using Number.ToString("### ##0.00") witch transfers real number to simple string with shown string format and round it to two decimals:


string Text = " ";
double Number = 0;
bool IsReal = false;

Text = textBox1.Text;
IsReal = double.TryParse(Text,out Number);
if (IsReal)
{
   Number = Math.Round(Number,2);
   textBox1.Text = Number.ToString("### ##0.00");
}
else
{
   MessageBox.Show("You did not entered the number !");
}



在Number.ToString()中使用字符串格式"### ## 0.00",
您会将数字四舍五入到两位小数,因此通常不需要使用
Math.Round(Number,2),并且千位之间用空格分隔:

2.30
123.56
1 236.30
100 001.65


尝试阅读编程语言中有关字符串格式的帮助说明.还有很多要学习的内容.

废除使用此解决方案提交的代码仅适用于双重类型的变量...

祝一切顺利,
PerićŽeljko



Using string format "### ##0.00" inside the Number.ToString(),
you get rounded number to two decimal places so you usually do not need to use
Math.Round(Number,2), and the thousands are separated by space :

2.30
123.56
1 236.30
100 001.65
etc.

Try reading help instructions for string formatting inside your''s programming language.There is a lot more to learn.

Bevare the code submited with this solution only works for double type of variables...

All the best,
Perić Željko


这篇关于如何在窗口应用程序中设置文本框值.....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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