C#.Net工作笔记006---关于各种四舍五入_小数点保留第几位等操作_随时更新
发布日期:2021-06-29 18:07:26
浏览次数:2
分类:技术文章
本文共 1277 字,大约阅读时间需要 4 分钟。
今天做项目,各种切小数点,金融中可能用的不是4舍5入,可能是4舍6入5成双.
1.关于截取小数点第几位(保留几位小数,非4舍5入),直接舍去
用下面的方法可以实现,一个方法,一个重载方法
public static decimal CutDecimalWithN(decimal d, int n) { string strDecimal = d.ToString(); int index = strDecimal.IndexOf("."); if (index == -1 || strDecimal.Length < index + n + 1) { strDecimal = string.Format("{0:F" + n + "}", d); } else { int length = index; if (n != 0) { length = index + n + 1; } strDecimal = strDecimal.Substring(0, length); } return Decimal.Parse(strDecimal); } public static decimal CutDecimalWithN(decimal? d, int n) { if (d==null) { return Decimal.MinValue; } return CutDecimalWithN(Convert.ToDecimal(d), n); }
2. decimal.Round 四舍五入和银行家算法的(四舍六入五成双)
decimal.Round(xx, decimalPos, MidpointRounding.ToEven);
VS
decimal.Round(xx, decimalPos, MidpointRounding.AwayFromZero);
AwayFromZero, 就是常用的四舍五入.
ToEven, 就比较特别了,称为就近舍入或四舍六入五成双
e.g.
22.333 to 22.33
22.335 to 22.34 22.325 to 22.32 22.336 to 22.34 22.326 to 22.33后面是5的情况比较特殊,如果前面是奇数,则升上去,反之,则截掉,在金融结算中,经常使用该方法,来平衡双方的盈亏
转载地址:https://credream.blog.csdn.net/article/details/89479807 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年05月02日 08时20分48秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux——基础命令篇
2021-07-02
EL表达式、JSTL标签库、文件上传和下载
2021-07-02
Cookie、Session
2021-07-02
表单重复提交
2021-07-02
Filter
2021-07-02
微服务架构实施原理详解
2021-07-02
必须了解的mysql三大日志-binlog、redo log和undo log
2021-07-02
局部敏感哈希Locality Sensitive Hashing归总
2021-07-03
图像检索中为什么仍用BOW和LSH
2021-07-03
图˙谱˙马尔可夫过程˙聚类结构----by林达华
2021-07-03
深度学习读书笔记之AE(自动编码AutoEncoder)
2021-07-03
深度学习读书笔记之RBM
2021-07-03
深度学习word2vec笔记之基础篇
2021-07-03
法国INRIA Data Sets & Images 数据集和图像库
2021-07-03
训练自己haar-like特征分类器并识别物体(1)
2019-04-30
iOS容易造成循环引用的三种场景,就在你我身边!
2019-04-30
iOS下KVO使用过程中的陷阱
2019-04-30