Math.Round(x)通过程序测试了几个个数据,列表如下:x值 反回值 正常值(中国人会计习惯)
0.5 0 1
-0.5 0 -1
0.6 1 1
-0.6 -1 -1查了点资料,C#的Round是四舍六入五取偶查到了点网上转载多的代码:
double ChinaRound(double value, int decimals)
{
if (value < 0)
{
return Math.Round(value + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero);
}
else
{
return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
}
}
//这断代码处理不了-0.5四舍五入到整数,已测试 public static double Round(double d, int i)
{
if (d >= 0)
{
d += 5 * Math.Pow(10, -(i + 1));
}
else
{
d += -5 * Math.Pow(10, -(i + 1));
}
string str = d.ToString();
string[] strs = str.Split('.');
int idot = str.IndexOf('.');
string prestr = strs[0];
string poststr = strs[1];
if (poststr.Length > i)
{
poststr = str.Substring(idot + 1, i);
}
string strd = prestr + "." + poststr;
d = Double.Parse(strd);
return d;
}
//这断代码处理-0.5四舍五入到整数会报异常,已测试
0.5 0 1
-0.5 0 -1
0.6 1 1
-0.6 -1 -1查了点资料,C#的Round是四舍六入五取偶查到了点网上转载多的代码:
double ChinaRound(double value, int decimals)
{
if (value < 0)
{
return Math.Round(value + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero);
}
else
{
return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
}
}
//这断代码处理不了-0.5四舍五入到整数,已测试 public static double Round(double d, int i)
{
if (d >= 0)
{
d += 5 * Math.Pow(10, -(i + 1));
}
else
{
d += -5 * Math.Pow(10, -(i + 1));
}
string str = d.ToString();
string[] strs = str.Split('.');
int idot = str.IndexOf('.');
string prestr = strs[0];
string poststr = strs[1];
if (poststr.Length > i)
{
poststr = str.Substring(idot + 1, i);
}
string strd = prestr + "." + poststr;
d = Double.Parse(strd);
return d;
}
//这断代码处理-0.5四舍五入到整数会报异常,已测试
public static int GetInt(double a)
{
double _Value = 0;
if (a == 0) return 0;
if (a > 0)
{
_Value =a+ 0.9999999999999999;
}
else
{
_Value =a+ (-0.999999999999999);
}
return (int)_Value;
}
{
if (d >= 0)
{
d += 5 * Math.Pow(10, -(i + 1));
}
else
{
d += -5 * Math.Pow(10, -(i + 1));
}
if (i != 0)
{ string str = d.ToString();
string[] strs = str.Split('.');
int idot = str.IndexOf('.');
string prestr = strs[0];
string poststr = strs[1];
if (poststr.Length > i)
{
poststr = str.Substring(idot + 1, i);
}
string strd = prestr + "." + poststr;
d = Double.Parse(strd);
}
return d;
}异常解决了,可以-0.44449保留2位小数结果是-0.44,还是不对啊
{
return Math.Sign(d) * Math.Round(Math.Abs(d) + 1 * Math.Pow(10, -(i + 10)), i);
}
1.0
?Math.Round(-0.5,0,System.MidpointRounding.AwayFromZero)
-1.0
具体可以参考http://msdn.microsoft.com/zh-cn/library/system.midpointrounding.aspx
看下定义吧,对负数好像不行吧?using System.Runtime.InteropServices;namespace System
{
// 摘要:
// 指定数学舍入方法应如何处理两个数字间的中间值。
[ComVisible(true)]
public enum MidpointRounding
{
// 摘要:
// 当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。
ToEven = 0,
//
// 摘要:
// 当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。
AwayFromZero = 1,
}
}
用这个测试如下代码,结果是-0.44,也不对 private void button3_Click(object sender, EventArgs e)
{
double d = bRound(-0.44449, 2); MessageBox.Show(d.ToString());
}
好像0.44才是正确的,excel也是显示0.44
用D7测试了下,也是0.44,不是D7的rondto()用的啥标准
return Math.Round(value, decimals, MidpointRounding.AwayFromZero);刚经过测试,这个问题已经不存在了...我想应该是微软在.NET 2.0 SP1以后进行了修补...
// 当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。
AwayFromZero = 1,
------------------
不过这个描述上的错误仍然没有改...不知道微软中国的人都在干什么...你可以参考英文MSDN原文...AwayFromZero When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.你再去看看中文MSDN MidpointRounding 枚举中的描述...里面又清清楚楚地说...如果指定了 AwayFromZero,则将返回 -3,因为它是精度为零且与零最接近的数字。取绝对值较大的值
float ave = float.Parse((x).ToString("F2"));
我用的是VS2010版本,经过“四舍五入”测试:
Console.WriteLine(Math.Round(-0.5, 0, MidpointRounding.AwayFromZero));
输出为:-1