Access数据库中round方法四舍五入问题

来源:互联网 发布:js的相对定位 编辑:程序博客网 时间:2024/04/20 21:08

java中拼接的access数据库的sql中使用round方法进行四舍五入有时候遇到5会有问题,确切的说是四舍六入,5不一定。

后来看到有的用format方法代替round方法:如:

String sql = "select format(Zcf,'Fixed') as cs from table";

执行这段sql的时候java会报异常:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'i'at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:826)at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:634)at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:605)at com.hxtt.global.ao.a(Unknown Source)at com.hxtt.global.ao.a(Unknown Source)

这里就不全写了。


后来找到一种解决办法:还是用round方法:

地址:https://www.techonthenet.com/access/functions/numeric/round.php

在文章的最后一部分讲到了这一块的问题:

在 round方法的参数里加上0.0001,这样就能保证逢五必进一。


附原文(英文):(大概意思一目了然就不翻译了)

Question: I read your explanation of the Round function using the round-to-even logic. However, I really need to round some values in the traditional sense (where 5 always rounds up). How can I do this?

Answer: You could always use the following logic:

If you wanted to round 12.65 to 1 decimal place in the traditional sense (where 12.65 rounded to 1 decimal place is 12.7, instead of 12.6), try adding 0.000001 to your number before applying the Round function:

Round(12.45+0.000001,1)

By adding the 0.000001, the expression that you are rounding will end in 1, instead of 5...causing the Round function to round in the traditional way.

And the 0.000001 does not significantly affect the value of your expression so you shouldn't introduce any calculation errors.


0 0
原创粉丝点击