自己做一个四舍五入的function:
int f1(float fInput)
{
int iResult;
iResult = fInput - (int)fInput <= 0.4 ? (int)fInput : (int)fInput + 1;
return iResult;
}临时写的,没有测试过
int f1(float fInput)
{
int iResult;
iResult = fInput - (int)fInput <= 0.4 ? (int)fInput : (int)fInput + 1;
return iResult;
}临时写的,没有测试过
(int)(0.5+0.5)就是1,所以,对于从十分位四舍五入到个位只要在强行转换到int之前先加0.5,其他的位类似!
this.Label1.Text=x.ToString("f0");
显式转换到int类型小数点右边的值将丢失
Math.Round可以实现四舍五入。关于揭贴,你进入管理,就什么都知道了。
很聪明
我至少没想到
Math 类 | Math 成员 | System 命名空间
语言
C#C++JScriptVisual Basic全部显示
返回最接近指定值的数字。重载列表
返回最接近指定值的整数。[Visual Basic] Overloads Public Shared Function Round(Decimal) As Decimal
[C#] public static decimal Round(decimal);
[C++] public: static Decimal Round(Decimal);
[JScript] public static function Round(Decimal) : Decimal;
返回最接近指定值的整数。[Visual Basic] Overloads Public Shared Function Round(Double) As Double
[C#] public static double Round(double);
[C++] public: static double Round(double);
[JScript] public static function Round(double) : double;
返回具有指定精度、最接近指定值的数。[Visual Basic] Overloads Public Shared Function Round(Decimal, Integer) As Decimal
[C#] public static decimal Round(decimal, int);
[C++] public: static Decimal Round(Decimal, int);
[JScript] public static function Round(Decimal, int) : Decimal;
返回具有指定精度、最接近指定值的数。[Visual Basic] Overloads Public Shared Function Round(Double, Integer) As Double
[C#] public static double Round(double, int);
[C++] public: static double Round(double, int);
[JScript] public static function Round(double, int) : double;
示例
[Visual Basic, C#, C++] 下面的代码演示就近舍入。[Visual Basic, C#, C++] 注意 此示例显示如何使用 Round 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。
[Visual Basic]
Math.Round(3.44, 1) 'Returns 3.4.
Math.Round(3.45, 1) 'Returns 3.4.
Math.Round(3.46, 1) 'Returns 3.5.[C#]
Math.Round(3.44, 1); //Returns 3.4.
Math.Round(3.45, 1); //Returns 3.4.
Math.Round(3.46, 1); //Returns 3.5.[C++] Math::Round(3.44, 1); //Returns 3.4.
Math::Round(3.45, 1); //Returns 3.4.
Math::Round(3.46, 1); //Returns 3.5.[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的语言筛选器按钮 。
最接近 a 的整数。如果 a 在两个整数的中间,其中之一按定义是偶数,另一个是奇数,则返回偶数。此方法的行为遵循 IEEE 标准 754 的第 4 节。这种舍入有时称为就近舍入或银行家舍入
Math.Round(4.4); //Returns 4.0.
Math.Round(4.5); //Returns 4.0. <---仔细看清楚!!!
Math.Round(4.6); //Returns 5.0.