double d=12.3424;
MessageBox.Show(d.ToString(".##"));
MessageBox.Show(d.ToString(".##"));
解决方案 »
- 如何是button对象位于主窗体的右下角?
- c# 如何判断datagridview是那个单元格触发了cellvaluechanged事件?
- 求C# 高性能的图像锐化算法。flickr的锐化效果非常好啊。。。
- 小妹请问一个XML读取属性值的简单问题。
- 问个LINQ比如我表A中有DATE字段。我想按日分组,怎么写? group by的问题
- 求助:DataGridView如何设置行高,列宽及字体大小?
- 新项目,郁闷中
- DevPress GridControl如何添加超连接
- 关于itextSharp生成PDF
- 关于类型转换问题,回家再来看结果……拜托
- 遍歷DataSet,DataTable問題
- 急急!!将vb代码转为c#,
Math 类 | Math 成员 | System 命名空间 | Math.Round 重载列表 | Ceiling | Floor | Math 成员(Visual J# 语法) | C++ 托管扩展编程
要求
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版 - Windows CE .NET
语言
C#C++JScriptVisual Basic全部显示
返回具有指定精度、最接近指定值的数。[Visual Basic]
Overloads Public Shared Function Round( _
ByVal d As Decimal, _
ByVal decimals As Integer _
) As Decimal[C#]
public static decimal Round(
decimal d,
int decimals
);[C++]
public: static Decimal Round(
Decimal d,
int decimals
);[JScript]
public static function Round(
d : Decimal,
decimals : int
) : Decimal;参数
d
要舍入的 Decimal 数。
decimals
返回值中的有效小数位数(精度)。
返回值
精度等于 decimals 的、最接近 d 的数字。如果 d 位于两个数字的中间,其中一个为偶数,另一个为奇数,则返回偶数。如果 d 的精度小于 decimals,那么返回 d 而不做更改。备注
decimals 参数指定返回值的有效小数位数,其范围在 0 到 28 之间。如果 decimals 为零,则返回一个整数。此方法的行为遵循 IEEE 标准 754 的第 4 节。这种舍入有时称为就近舍入或银行家舍入。如果 decimals 为零,则这种舍入有时称为“向零舍入”。示例
下面的代码演示就近舍入。[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]
System.Math.Round(3.44, 1) //Returns 3.4.
System.Math.Round(3.45, 1) //Returns 3.4.
System.Math.Round(3.46, 1) //Returns 3.5.要求
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版 - Windows CE .NET
d=double.Parse(d.ToString(".##"));
是后来分开的,原来只有一句:myDouble=Math.Floor (myDouble*y)/y;。原来没分开由于在使用过程中发现如下问题:例如8.2取两位小数返回的是8.19,8.12返回8.11,8.29返回8.28等等,所以才分开。
问题就出在8.29*100=828.99999999999989,然后去掉所有小数位变成了828,再除以100则得到8.28。其它几个数也是一样的原因。后来我发现如果ToString一下828.99999999999989就会变成829,所以我分开。
但是我以前用这个函数希望达到同样的目的:
public double GetTrun(double myDouble,int Length)
{ string tempString;
tempString=myDouble.ToString ();
int startInt=tempString.IndexOf (".");
if (startInt==-1)
{
//r如果是整数,直接返回
return myDouble;
}
int tempint=tempString.Substring (startInt+1).Length ;//取小数点后共有几位
if (tempint<=Length)
{//如果小数点后的位数小于等于您要的精度数,则直接返回
return myDouble;
}
tempString=tempString.Substring(0,startInt+1+Length);
myDouble=Convert.ToDouble (tempString);
return myDouble;
}
结果就发现有些数比如上面的828.99999999999989还是返回原来的值,于是ToString一下就可以了。
但是,总是担心有些没有测试到的数据会出错。
不知道大家在编程时是如何处理这个问题的?
或者谁有经过测试的正确的函数给我一个,不胜感激!:)
d=double.Parse(d.ToString("F2"));
那你就直接截取...
try double d = 12.3424;
string st = d.ToString();
st = st.Substring(0,st.LastIndexOf(".")+3);
d = double.Parse(st);
public double GetTrun(double myDouble,int Length)
{ string tempString;
tempString=myDouble.ToString ();
int startInt=tempString.IndexOf (".");
if (startInt==-1)
{
//r如果是整数,直接返回
return myDouble;
}
int tempint=tempString.Substring (startInt+1).Length ;//取小数点后共有几位
if (tempint<=Length)
{//如果小数点后的位数小于等于您要的精度数,则直接返回
return myDouble;
}
tempString=tempString.Substring(0,startInt+1+Length);
myDouble=Convert.ToDouble (tempString);
return myDouble;
}
d=double.Parse(d.ToString("F2"));
d=double.Parse(d.ToString(".##"));
上面这些是否都是四舍五入?(根据我得测试是),但是客户要求两位小数后的数字不管是几都要舍去,就是9也不例外即也要舍去。
这样诸位的回答不能够满足我得要求啊。
请大家务必理解我得要求:必须是两位小数后全部舍去。
此贴与http://community.csdn.net/Expert/topic/3238/3238212.xml?temp=.4941828贴是一个意思.都是我发的.