社区
C#
帖子详情
紧急!求助关于程序四舍六入问题,各位达人帮忙
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)
[
紧急
求助
]如何用
程序
实现外部提交?针对用asp实现的文件上传
程序
...内网
程序
...
[
问题
描述]是这样的,好朋友所在公司,要他处理这样的业务:通过内网的一个asp写的
程序
,不断地上传文件到服务器(只知道服务器地址,但数据库存放地址和类型皆未知),每次上传时还输入几个其他信息。上传的文件...
我在CSDN参与的3000个帖子
2:100分急求,随机输出十个小写字母,但是,要求这十个字母不相同 3:求Sn=a+aa+aaa+…+aaa…a(n个a)之值 4:数组题 望高手
帮忙
! 5:呵呵,来推荐一下我的网站,本站提供大量当今流行的免费的音乐和免费电影,...
各位佬们,该从哪里找FPGA的八股啊,想准备一下之后好找实习
浏览器发送一起请求的执行流程是 各位大佬们
求助
,记忆科技(东莞)和得一微 各位大佬们
求助
,记忆科技(东莞)和得一微电子(深圳)。两个岗位都是固件开发,薪资差不多。 比亚迪-人才库 10.29线下一面10.31线上二...
WEB开发文档2 总结
http://blog.donews.com/lvjiyong/archive/2006/06/29/931071.aspx怎样将后台生成的在内存中的图象显示到客户端Microsoft IE WebControls下载...显示数据下载中文名文件时保存文件名乱码
问题
关于用ASP.net绘图的
问题
...
Thinkpad常见
问题
大全(转载联想工程师博客)
A:这个
问题
的补丁
程序
目前似乎还没有发布……另外一个分区也不恐怖,我自己用过的多少ThinkPad都是一个分区呢。而且R&R对系统的保护确实也很不错,一键恢复您可以从R&R备份恢复,而不见得要恢复到出厂设置啊,速度...
C#
110,499
社区成员
642,566
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章