我是靠谱客的博主 聪明滑板,最近开发中收集的这篇文章主要介绍BigDecimal用double初始化并设置精确度的问题,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

为什么80%的码农都做不了架构师?>>>   hot3.png

System.out.println(new BigDecimal(9.99)); // 9.9900000000000002131628207280300557613372802734375
System.out.println(new BigDecimal(9.99).setScale(2, BigDecimal.ROUND_CEILING)); // 10.00
System.out.println(new BigDecimal(9.55)); // 9.550000000000000710542735760100185871124267578125
System.out.println(new BigDecimal(9.55).setScale(2, BigDecimal.ROUND_CEILING)); // 9.56
System.out.println(new BigDecimal(9.1)); // 9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(9.1).setScale(2, BigDecimal.ROUND_CEILING)); // 9.10
System.out.println(new BigDecimal(-9.44)); // -9.4399999999999995026200849679298698902130126953125
System.out.println(new BigDecimal(-9.44).setScale(2, BigDecimal.ROUND_CEILING)); // -9.43
System.out.println(new BigDecimal(-9.11)); // -9.1099999999999994315658113919198513031005859375
System.out.println(new BigDecimal(-9.11).setScale(2, BigDecimal.ROUND_CEILING)); // -9.10
System.out.println(new BigDecimal(-9.1)); // -9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(-9.1).setScale(2, BigDecimal.ROUND_CEILING)); // -9.09
System.out.println(new BigDecimal(9.44)); // 9.4399999999999995026200849679298698902130126953125
System.out.println(new BigDecimal(9.44).setScale(2, BigDecimal.ROUND_DOWN)); // 9.43
System.out.println(new BigDecimal(9.11)); // 9.1099999999999994315658113919198513031005859375
System.out.println(new BigDecimal(9.11).setScale(2, BigDecimal.ROUND_DOWN)); // 9.10
System.out.println(new BigDecimal(9.1)); // 9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(9.1).setScale(2, BigDecimal.ROUND_DOWN)); // 9.09
System.out.println(new BigDecimal(-9.44)); // -9.4399999999999995026200849679298698902130126953125
System.out.println(new BigDecimal(-9.44).setScale(2, BigDecimal.ROUND_DOWN)); // -9.43
System.out.println(new BigDecimal(-9.11)); // -9.1099999999999994315658113919198513031005859375
System.out.println(new BigDecimal(-9.11).setScale(2, BigDecimal.ROUND_DOWN)); // -9.10
System.out.println(new BigDecimal(-9.1)); // -9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(-9.1).setScale(2, BigDecimal.ROUND_DOWN)); // -9.09
System.out.println(new BigDecimal(9.44)); // 9.4399999999999995026200849679298698902130126953125
System.out.println(new BigDecimal(9.44).setScale(2, BigDecimal.ROUND_FLOOR)); // 9.43
System.out.println(new BigDecimal(9.11)); // 9.1099999999999994315658113919198513031005859375
System.out.println(new BigDecimal(9.11).setScale(2, BigDecimal.ROUND_FLOOR)); // 9.10
System.out.println(new BigDecimal(9.1)); // 9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(9.1).setScale(2, BigDecimal.ROUND_FLOOR)); // 9.09
System.out.println(new BigDecimal(-9.99)); // -9.9900000000000002131628207280300557613372802734375
System.out.println(new BigDecimal(-9.99).setScale(2, BigDecimal.ROUND_FLOOR)); // -10.00
System.out.println(new BigDecimal(-9.55)); // -9.550000000000000710542735760100185871124267578125
System.out.println(new BigDecimal(-9.55).setScale(2, BigDecimal.ROUND_FLOOR)); // -9.56
System.out.println(new BigDecimal(-9.1)); // -9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(-9.1).setScale(2, BigDecimal.ROUND_FLOOR)); // -9.10
System.out.println(new BigDecimal(9.1)); // 9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(9.1).setScale(2, BigDecimal.ROUND_HALF_DOWN)); // 9.10
System.out.println(new BigDecimal(-9.1)); // -9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(-9.1).setScale(2, BigDecimal.ROUND_HALF_DOWN)); // -9.10
System.out.println(new BigDecimal(9.1)); // 9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(9.1).setScale(2, BigDecimal.ROUND_HALF_EVEN)); // 9.10
System.out.println(new BigDecimal(-9.1)); // -9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(-9.1).setScale(2, BigDecimal.ROUND_HALF_EVEN)); // -9.10
System.out.println(new BigDecimal(9.1)); // 9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(9.1).setScale(2, BigDecimal.ROUND_HALF_UP)); // 9.10
System.out.println(new BigDecimal(-9.1)); // -9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(-9.1).setScale(2, BigDecimal.ROUND_HALF_UP)); // -9.10
System.out.println(new BigDecimal(9.99)); // 9.9900000000000002131628207280300557613372802734375
System.out.println(new BigDecimal(9.99).setScale(2, BigDecimal.ROUND_UP)); // 10.00
System.out.println(new BigDecimal(9.55)); // 9.550000000000000710542735760100185871124267578125
System.out.println(new BigDecimal(9.55).setScale(2, BigDecimal.ROUND_UP)); // 9.56
System.out.println(new BigDecimal(9.1)); // 9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(9.1).setScale(2, BigDecimal.ROUND_UP)); // 9.10
System.out.println(new BigDecimal(-9.99)); // -9.9900000000000002131628207280300557613372802734375
System.out.println(new BigDecimal(-9.99).setScale(2, BigDecimal.ROUND_UP)); // -10.00
System.out.println(new BigDecimal(-9.55)); // -9.550000000000000710542735760100185871124267578125
System.out.println(new BigDecimal(-9.55).setScale(2, BigDecimal.ROUND_UP)); // -9.56
System.out.println(new BigDecimal(-9.1)); // -9.0999999999999996447286321199499070644378662109375
System.out.println(new BigDecimal(-9.1).setScale(2, BigDecimal.ROUND_UP)); // -9.10


转载于:https://my.oschina.net/morven/blog/533021

最后

以上就是聪明滑板为你收集整理的BigDecimal用double初始化并设置精确度的问题的全部内容,希望文章能够帮你解决BigDecimal用double初始化并设置精确度的问题所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部