紧急!求助关于程序四舍六入问题,各位达人帮忙

fogwater 2006-06-12 05:46:25
我现在一个做一个项目,用户提出了一个奇怪的要求,即四舍六入。
具体描述如下:一般情况下,如果要求保留两位小数,程序为自动采用四舍五入的方法。
现在问题是用户要求采用四舍六入方法进行进位。
有什么好的解决方法,各位达人请不吝指教,小弟不胜感激
...全文
396 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
teacher1998 2006-06-14
  • 打赏
  • 举报
回复
这世界好奇怪:以前大家穷,0(你)不要了,这个正常;后来有吃的了,5(吾)以下零头不要了,现在富有了,连5(吾)也不要了,我看什么时候会连8(爸)也不要了!
feixue_XXXX 2006-06-13
  • 打赏
  • 举报
回复
上面代码零时写的,有好多不到之处请谅解!希望项目愉快!
feixue_XXXX 2006-06-13
  • 打赏
  • 举报
回复
/// <summary>
/// 转化
/// </summary>
/// <param name="OrignNum">要转换的数值</param>
/// <param name="n">位数</param>
/// <returns>转换后的字符串</returns>
protected string ZH(double OrignNum,int n)
{
string[] str=OrignNum.ToString().Split(new char[]{'.'});
if(str.Length<1 || str.Length>2)
{
return Math.PI.ToString();// 你可以替换为你认为不可能的数值
}
char[] splitNum=str[1].ToCharArray();
if(n>=splitNum.Length)
{
return OrignNum.ToString();
}
int SplitW=int.Parse(splitNum[n].ToString());

if(SplitW>=6)
{
int v=str[1].Substring(0,n).Length;

str[1]=(int.Parse(str[1].Substring(0,n))+1).ToString();

if(v<str[1].Length)
{
str[1]=str[1].Substring(1);
str[0]=(int.Parse(str[0])+1).ToString();
}
}
else
{
str[1]=str[1].Substring(0,n);
}
return str[0]+"."+str[1];
}
代码粗略验,如有bug请指正
cscer 2006-06-13
  • 打赏
  • 举报
回复
默认就是四舍六入五留双吧

我先试试
jackyped 2006-06-13
  • 打赏
  • 举报
回复
4舍6入5保留?
看你们客户想怎么操作5
lookatliu 2006-06-13
  • 打赏
  • 举报
回复
lz把例子给出来让大家明白才能做啊。
比如:
5.5
5.6
6.5
6.6
这些转换之后等于多少。
socg 2006-06-13
  • 打赏
  • 举报
回复
Round--小数小于.5舍,大于.5入,等于.5则是向偶数方向入,这才是公平的!

我还是在想楼主的四舍六入,5怎么办!
amandag 2006-06-13
  • 打赏
  • 举报
回复
客户是上帝,先up
fogwater 2006-06-13
  • 打赏
  • 举报
回复
呵呵,我上网搜了一下,发现了一个
四舍五入 与 四舍六入五成双 C#,vs.net.j#,vbscript都是采用的四舍六入五成双
SQL server 使用的是四舍五入

double aa = 1.25;
aa = Math.Round(aa,1);
Response.Write(aa.ToString());
返回的是1.2

SQL 中 select round(1.25,1) 返回的是1.3
Kevin_jun 2006-06-13
  • 打赏
  • 举报
回复
来源:http://msdn2.microsoft.com/zh-cn/library/system.midpointrounding.aspx
2.0才有四舍六入五成双这样的例子.1.0中我试了下没有.

// This example demonstrates the Math.Round() method in conjunction
// with the MidpointRounding enumeration.
using System;

class Sample
{
public static void Main()
{
decimal result = 0.0m;
decimal posValue = 3.45m;
decimal negValue = -3.45m;

// By default, round a positive and a negative value to the nearest even number.
// The precision of the result is 1 decimal place.

result = Math.Round(posValue, 1);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue);
result = Math.Round(negValue, 1);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue);
Console.WriteLine();

// Round a positive value to the nearest even number, then to the nearest number away from zero.
// The precision of the result is 1 decimal place.

result = Math.Round(posValue, 1, MidpointRounding.ToEven);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, posValue);
result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, posValue);
Console.WriteLine();

// Round a negative value to the nearest even number, then to the nearest number away from zero.
// The precision of the result is 1 decimal place.

result = Math.Round(negValue, 1, MidpointRounding.ToEven);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, negValue);
result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, negValue);
Console.WriteLine();
}
}
/*
This code example produces the following results:

3.4 = Math.Round( 3.45, 1)
-3.4 = Math.Round(-3.45, 1)

3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)

*/

win911 2006-06-13
  • 打赏
  • 举报
回复
C#的Math.Rount实现的是四舍六入五成双的原则
Console.WriteLine(Math.Round(11.4).ToString());=11
Console.WriteLine(Math.Round(11.5).ToString());=12
Console.WriteLine(Math.Round(11.6).ToString());=12
Console.WriteLine(Math.Round(10.5).ToString());=10

也就是说当是5的时候将取最接近的偶数。
kyle315 2006-06-12
  • 打赏
  • 举报
回复
不太明白楼主的意思,四舍六入,如果是6.5怎么办,你要的结果是6还是7?
其实Math.Round就是五舍六入的

aliketen 2006-06-12
  • 打赏
  • 举报
回复
//对价格进行四舍五入
string myPrice = String.Format("{0:N2}", Math.Round((Convert.ToDouble(price)*Convert.ToDouble(discount)*0.1),2));
amendajing 2006-06-12
  • 打赏
  • 举报
回复
Math.Round
skyline81 2006-06-12
  • 打赏
  • 举报
回复
round 是国际标准进行的四舍五入
lookatliu 2006-06-12
  • 打赏
  • 举报
回复
我的意思是6.6和5.6你强转的结果不一样的,都是按国际标准进行的四舍五入。
ralpha08 2006-06-12
  • 打赏
  • 举报
回复
ls的爱钻牛角尖
我说了 不管什么值都加0.4再int 那么对于
int bbbb = (int)(ccc + 1.5);
应该是
int bbbb = (int)(ccc + 1.5+0.4);

lookatliu 2006-06-12
  • 打赏
  • 举报
回复
double ccc = 5.1;
int aaaa = (int)(ccc+0.5);
int bbbb = (int)(ccc + 1.5);
ls请看看bbbb是什么值,应该是7,确是6。
ralpha08 2006-06-12
  • 打赏
  • 举报
回复
不管是什么数都是+0.4再int
ralpha08 2006-06-12
  • 打赏
  • 举报
回复
4.7+0.4=5.1
再int就是5
加载更多回复(4)

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧