社区
C#
帖子详情
紧急!求助关于程序四舍六入问题,各位达人帮忙
fogwater
2006-06-12 05:46:25
我现在一个做一个项目,用户提出了一个奇怪的要求,即四舍六入。
具体描述如下:一般情况下,如果要求保留两位小数,程序为自动采用四舍五入的方法。
现在问题是用户要求采用四舍六入方法进行进位。
有什么好的解决方法,各位达人请不吝指教,小弟不胜感激
...全文
401
24
打赏
收藏
紧急!求助关于程序四舍六入问题,各位达人帮忙
我现在一个做一个项目,用户提出了一个奇怪的要求,即四舍六入。 具体描述如下:一般情况下,如果要求保留两位小数,程序为自动采用四舍五入的方法。 现在问题是用户要求采用四舍六入方法进行进位。 有什么好的解决方法,各位达人请不吝指教,小弟不胜感激
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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写的
程序
,不断地上传文件到服务器(只知道服务器地址,但数据库存放地址和类型皆未知),每次上传时还输入几个其他信息。上传的文件放在一个目录里。(以下上传页面保存回来的静态页面:http://files.cnblogs.com/lxinxuan/faxfileupdatebycrm.rar)上传界面如下所示:[好友的烦恼]这...
我在CSDN参与的3000个帖子
今日偶然翻到,感慨万千 1:申述:版主,是否扣了我的专家分? 2:100分急求,随机输出十个小写字母,但是,要求这十个字母不相同 3:求Sn=a+aa+aaa+…+aaa…a(n个a)之值 4:数组题 望高手
帮忙
! 5:呵呵,来推荐一下我的网站,本站提供大量当今流行的免费的音乐和免费电影,常用软件、游戏、精美图库下载,希望对网友有帮助!顺便散分! 6:VBA请教怎
各位佬们,该从哪里找FPGA的八股啊,想准备一下之后好找实习
技术一面(60min)1、拷问项目:阴影怎么做的,有做阴影烘培吗,车辆行驶怎么做的2、拷问实习:抽奖活动怎么做出来的3、给一个排好序的数组,如何构造平衡搜索树,孩子真的很想回西安,奈何一直没有合适的岗位base在南方的公司全给offer了base在北方的奈何一个都不理我孩子要哭了,有没有什么西安的好岗位大家推荐一下啊,弹性工作制 不打卡上班!顺丰,蓝禾,宁德时代,蓝月亮,康龙化成,三七互娱,奇安信24秋招①顺丰集团【招聘岗位】研发类、管培生类,航空类,经营管理、职能【内推码】4FOLXH【一键内推】
WEB开发文档2 总结
转自:http://blog.donews.com/lvjiyong/archive/2006/06/29/931071.aspx怎样将后台生成的在内存中的图象显示到客户端Microsoft IE WebControls下载地址如何在DATAGRID中使用JAVASCRIPT脚本控制DataGrid中连接到下一页显示数据下载中文名文件时保存文件名乱码
问题
关于用ASP.net绘图的
问题
,请大虾指教那
Thinkpad常见
问题
大全(转载联想工程师博客)
想要收藏本篇文章请下载Word版 Q:我想升级成VISTA,想问一下,升级之后一键恢复是恢复到XP还是VISTA?A:如果从隐含分区恢复出厂设置,那当然是恢复到出厂时预装的系统;如果是用R&
C#
111,093
社区成员
642,554
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章