我想用数学来做。圆的函数
当前回答
//转换到小数点后两位
String.Format("{0:0.00}", 140.6767554); // "140.67"
String.Format("{0:0.00}", 140.1); // "140.10"
String.Format("{0:0.00}", 140); // "140.00"
Double d = 140.6767554;
Double dc = Math.Round((Double)d, 2); // 140.67
decimal d = 140.6767554M;
decimal dc = Math.Round(d, 2); // 140.67
= = = = = = = = =
// just two decimal places
String.Format("{0:0.##}", 123.4567); // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
String.Format("{0:0.##}", 123.0); // "123"
也可以将“0”和“#”组合。
String.Format("{0:0.0#}", 123.4567) // "123.46"
String.Format("{0:0.0#}", 123.4) // "123.4"
String.Format("{0:0.0#}", 123.0) // "123.0"
其他回答
字符串a = "10.65678";
decimal d = Math.Round(Convert.ToDouble(a.ToString()),2)
您应该能够使用Math指定要舍入的位数。轮(YourNumber, 2)
你可以在这里阅读更多。
public double RoundDown(double number, int decimalPlaces)
{
return Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces);
}
下面是一些例子:
decimal a = 1.994444M;
Math.Round(a, 2); //returns 1.99
decimal b = 1.995555M;
Math.Round(b, 2); //returns 2.00
你可能还想看看银行家的四舍五入/四舍五入到偶数与以下过载:
Math.Round(a, 2, MidpointRounding.ToEven);
这里有更多的信息。
这是为了在c#中四舍五入到小数点后2位:
label8.Text = valor_cuota .ToString("N2") ;
在VB。NET:
Imports System.Math
round(label8.text,2)
推荐文章
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 如何舍入一个双到最近的Int在迅速?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?
- c#对象列表,我如何得到一个属性的和
- 我如何使用IValidatableObject?
- 如何指定最小值,但没有使用范围数据注释属性的最大小数?
- c# vs Java Enum(适合c#新手)
- c#消毒文件名
- 在EF中更新父实体时如何添加/更新子实体
- ASP。NET身份的默认密码散列器-它是如何工作的,它是安全的?
- 如何写一个JSON文件在c# ?
- 静态隐式运算符