十进制值 [英] Decimal value
本文介绍了十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本框.如何使文本框仅接受小数点后两位数字,尽管在数据库中,该值包含小数点后两位以上的数字?
如果输入5,则应为5.00.
类似地:
对于5.1,应为5.10
对于5.1234,它应为5.12
希望您能为我提供解决方案.
Hi,
I have a textbox. How can I make a textbox to accept only two digits after the decimal, although in the database, the value contains more than two digits after the decimal?
If I enter 5, it should take as 5.00.
Similarly:
for 5.1 it should take as 5.10
for 5.1234 it should take as 5.12
Hope you''ll suggest me a solution.
推荐答案
示例
double p = 123456.12345678;
double p1 = Math.Round(p,2);
MessageBox.Show(p1.ToString());
如果有帮助,请投票并接受答案.
Please vote and Accept Answer if it Helped.
如果您要使用Windows窗体应用程序.
内部TextBox离开事件
If you are asking for Windows forms apps.
Inside TextBox Leave Event
private void textBox1_Leave(object sender, System.EventArgs e)
{
TextBox1.Text = double.Parse(TextBox1.Text).ToString("f2");
}
如果有帮助,请 投票 和 接受答案 .
Please vote and Accept Answer if it Helped.
如果有帮助数据库中的十进制数,那么它只是一个小数而未应用任何格式.如果只希望看到两个小数,则在检索该值时以及在显示该值之前,请切掉所有其他小数.
If it''s a decimal in the database, then it''ll simply be a fractional number without any formatting applied. If you only want to see two decimals, then cut off all other decimals when retrieving the value and before displaying it.
这篇关于十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文