如何在C#.NET中舍入双数字
问题描述:
嗨亲爱的。
我的表单中有2个TextBox。
我想要将TextBox1的数量四舍五入,并在。之后将其显示为带有4位数的TextBox2。
Hi dears.
I have 2 TextBox in my form.
I want to round the number of TextBox1 and show it into TextBox2 with 4 digit after"."
答
.ToString(F4)将为您提供一个带有4个固定数字的格式化字符串:
.ToString("F4") will give you a formatted string with 4 fixed digits:
string fromTextBox = TextBox1.Text;
double num = 0;
if(double.TryParse(fromTextBox, out num){
// Parsed from string ok..
string formattedNumber = num.ToString("F4");
}
decimal b = 1.55555555
Math.Round(b, 4); //returns 1.5556
查看此示例
see this example
double a = "2222.2349";//This is Input
string b = Math.Round(a, 3).ToString("0000.0000");
//-> 2222.2350
string c = Math.Round(a, 2).ToString("0000.0000");
//-> 2222.2300
string d = Math.Round(a, 1).ToString("0000.0000");
//-> 2222.2000
string f = Math.Round(a, 0).ToString("0000.0000");
//-> 2222.0000
快乐编码!
:)
Happy Coding!
:)