如何在C#.NET中舍入双数字 [英] How to round double numbers in C#.NET

查看:73
本文介绍了如何在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!
:)


这篇关于如何在C#.NET中舍入双数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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