round()函数在MySQL中的使用

ROUND(X), ROUND(X,D)
Rounds the argument X to D decimal places. The rounding algorithm depends on the data type of
X. D defaults to 0 if not specified. D can be negative to cause D digits left of the decimal point of the
value X to become zero.
mysql> SELECT ROUND(-1.23);
-> -1
mysql> SELECT ROUND(-1.58);
-> -2
mysql> SELECT ROUND(1.58);
Mathematical Functions
1839
-> 2
mysql> SELECT ROUND(1.298, 1);
-> 1.3
mysql> SELECT ROUND(1.298, 0);
-> 1
mysql> SELECT ROUND(23.298, -1);
-> 20
The return value has the same type as the first argument (assuming that it is integer, double, or
decimal). This means that for an integer argument, the result is an integer (no decimal places):
mysql> SELECT ROUND(150.000,2), ROUND(150,2);
+------------------+--------------+
| ROUND(150.000,2) | ROUND(150,2) |
+------------------+--------------+
| 150.00 | 150 |
+------------------+--------------+
ROUND() uses the following rules depending on the type of the first argument:
• For exact-value numbers, ROUND() uses the “round half away from zero” or “round toward
nearest” rule: A value with a fractional part of .5 or greater is rounded up to the next integer if
positive or down to the next integer if negative. (In other words, it is rounded away from zero.) A
value with a fractional part less than .5 is rounded down to the next integer if positive or up to the
next integer if negative.
• For approximate-value numbers, the result depends on the C library. On many systems, this
means that ROUND() uses the “round to nearest even” rule: A value with a fractional part exactly
halfway between two integers is rounded to the nearest even integer.
The following example shows how rounding differs for exact and approximate values:
mysql> SELECT ROUND(2.5), ROUND(25E-1);
+------------+--------------+
| ROUND(2.5) | ROUND(25E-1) |
+------------+--------------+
| 3 | 2 |
+------------+--------------+

round()函数在Java中的使用

     System.out.println(Math.round(2.5));System.out.println(Math.round(2.6));System.out.println(Math.round(-2.5));System.out.println(Math.round(-2.6));System.out.println(Math.round(-0.6));System.out.println(Math.round(-0.5));
         ans:33-2-3-10

原理:Math.round() = Math.floor(x + 0.5)

银行家四舍五入

多年来,许多人向我指出,VBScript的Round 函数有点奇怪。看起来应该很简单-您选择最接近故事末尾的整数。但是,比如说1.5?有两个最接近的整数。你会上升还是下降?

该回合功能进入到最接近的整数,并且如果有最接近的两个整数然后将其转到甚至一个。  1.5舍入为2,0.5舍入为0。

为什么?为什么不随便说在这种情况下我们总是四舍五入呢?为什么有时四舍五入而又向上舍入?实际上有一个很好的理由!

此算法称为“ 银行家舍入算法”,因为毫不奇怪,银行家使用该算法。假设数据源提供的数据通常是完全分割的数量(一半美元,一半美分,一半份额,等等),但他们希望提供四舍五入的数量。进一步假设数据使用者将要从四舍五入的数据中得出摘要统计信息,即平均值。

理想情况下,当您要取平均值时,您想要尽可能精确地取平均原始数据。但是在现实世界中,我们经常必须对丢失了某些精度的数据取平均值。在这种情况下,Banker的舍入算法会产生更好的结果,因为它不会使半数量的样本始终偏低或持续偏高。假设平均而言,相等数量的半数量将向下取整,并且误差将被抵消。

如果您不相信我,请尝试一下。生成以0.5结尾的随机数字列表,将其四舍五入并取平均值。您会发现,与“总是四舍五入”平均相比,“银行家四舍五入”为您提供了更接近真实平均的结果。

VBScript中的Round ,CInt 和CLng 函数均使用Banker的Rounding算法。

还有两个其他的VBScript函数可将浮点数转换为整数。的诠释功能给你的第一个整数小于或等于它的输入,并且所述修复功能给你的第一个整数更接近零或等于它的输入。这些函数根本不舍入到最接近的整数,它们只是截断小数部分。

link: https://blogs.msdn.microsoft.com/ericlippert/2003/09/26/bankers-rounding/

Bankers Rounding

Bankers Rounding is an algorithm for rounding quantities to integers, in which numbers which are equidistant from the two nearest integers are rounded to the nearest even integer. Thus, 0.5 rounds down to 0; 1.5 rounds up to 2. A similar algorithm can be constructed for rounding to other sets besides the integers (in particular, sets which a constant interval between adjacent members).

Other decimal fractions round as you would expect--0.4 to 0, 0.6 to 1, 1.4 to 1, 1.6 to 2, etc. Only x.5 numbers get the "special" treatment.

So called because banks supposedly use it for certain computations.

The supposed advantage to bankers rounding is that it is unbiased, and thus produces better results with various operations that involve rounding.

It should be noted that it is unbiased only in the limit. That is, an average of all errors approaches 0.0.

http://blogs.msdn.com/ericlippert/archive/2003/09/26/53107.aspx

https://github.com/WardCunningham/remodeling

link: http://wiki.c2.com/?BankersRounding

round()函数在Java和MySQL中的使用相关推荐

  1. MySQL自定义函数的使用及MySQL中的流程控制语句

    MySQL自定义函数 文章目录 MySQL自定义函数 创建自定义函数 自定义函数的调用 自定义函数的删除 自定义函数的维护 流程控制语句 条件控制语句 循环语句 创建自定义函数 创建自定义函数时, 开 ...

  2. mysql5.6 函数索引_聊聊MySQL中的索引

    关于MySQL中的索引使用 索引是数据库优化中最常用也是最重要的手段之一,通过索引通常可以帮助用户解决大多数的SQL性能问题. 索引的存储分类: 1.B-Tree索引:最常见的索引类型,大部分引擎都支 ...

  3. python中round函数参数有负数_Python中round函数使用注意事项

    使用round函数的时候发现了一个奇怪的现象.一直觉得round函数是一个用于四舍五入的函数,结果却不一定.一般如果觉得奇怪,那就是没弄懂其本质的运作原理,所以深入了解了下round函数. Pytho ...

  4. mysql节假日函数_如何在MySQL中计算不包括周末和节假日的日期差

    我需要计算两个日期之间的天数(工作日),不包括周末(最重要)和假期 SELECT DATEDIFF(end_date, start_date) from accounts 但是,我不知道该如何在MyS ...

  5. Java 和 Mysql中的时间格式化

    Java中的Date类和Java9中的增强日期类已经提供了很多日期格式,但是在web项目中经常需要String和日期格式相互转化,这个转化主要是在Java代码还有SQL语句中出现,这里就是记录一下这个 ...

  6. mysql数据库div函数_关于使用mysql中的div函数报错?报错-问答-阿里云开发者社区-阿里云...

    数据库MySQL 5.5.27 jar包:mysql-connector-java-5.1.21.jar mybatis-spring-1.1.1.jar druid-0.2.10.jar 集成myb ...

  7. java和mysql中md5+base64的执行结果

    目录 前言 java md5+base64 java md5+base64:commons-codec java md5+base64:shiro-core java covertToString(m ...

  8. java从mysql中查数据_java怎么从数据库中查询数据并输出

    try { //这里的是MYSQL 举例 //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //创建数据库连接 Connection c ...

  9. java处理 mysql中json类型

    当数据库中存在json类型的数据,如图 json类型的值在数据库中保存的时候,会先字母排序并加空格后保存 场景:业务上需要校验,此json字段是否跟库里的json完全匹配(验重) 原理:利用mysql ...

最新文章

  1. 知识图谱如何让智能金融“变魔术”
  2. matlab pca和逆pca函数,matlab_PCA,训练集与测试集分开,原理和用法
  3. dll加载问题的解决方法
  4. php 编译记录文件,php-7.1编译记录
  5. 山寨高仿iPad难现山寨手机的辉煌
  6. hyperledger fabric PBFT算法简要解析
  7. 比赛结果展示时,某些文字过长,如何使其换行又不影响美观?
  8. access orcad 数据库_OrCAD Capture CIS使用MySQL数据库
  9. 32 道常见的 Kafka 面试题
  10. html 刷新页面,javascript刷新页面的几种方法
  11. Python做出来的数据可视化真香!!
  12. Java实现163邮箱发送邮件到QQ邮箱
  13. 微服务调用链追踪中心搭建
  14. 泛泰 A870 4.1.2 刷第三方专用Recovery合集
  15. 响应式Web设计:HTML5和CSS3实战
  16. php扩展-ioncube组件的安装方法_如何安装ioncube扩展
  17. openstack部署过程5
  18. 磨金石教育摄影技能干货分享|那些酷炫的照片是怎么拍出来的?
  19. lol服务器位置2017,LOL怎么转区 LOL转区方法 LOL转区系统2017最新教程
  20. 小白学 Python 爬虫(11):urllib 基础使用(一)

热门文章

  1. 三分钟教你增值税发票怎么识别
  2. 机房效果图制作|简易制作教程赘述
  3. ByPASS系列之安全狗
  4. push_back使用方法
  5. Qt中的默认文件夹路径获取方法
  6. Vue整合到Spring Boot
  7. STM32 ST-LINK Utility解决错误提示“Can not read memory!Disable Read Out Protection and retry”
  8. [业余知识:PC 硬件监控]使用WinRing0 2.0.0.20读取CPU温度
  9. 天翼云电脑打造极致流畅与安全 助企业数字办公升级
  10. 土壤水力参数 matlab,一种由土壤剖面含水率估算土壤水力参数的方法