Title: BigDecimal文档里,建议对保留规则设置为ROUND_HALF_EVEN · Issue #2129 · Snailclimb/JavaGuide · GitHub
Open Graph Title: BigDecimal文档里,建议对保留规则设置为ROUND_HALF_EVEN · Issue #2129 · Snailclimb/JavaGuide
X Title: BigDecimal文档里,建议对保留规则设置为ROUND_HALF_EVEN · Issue #2129 · Snailclimb/JavaGuide
Description: 文档里BigDecimalUtil类选择的保留规则是RoundingMode.HALF_UP,即四舍五入。代码部分如下: /** * 提供精确的小数位四舍五入处理。 * * @param v 需要四舍五入的数字 * @param scale 小数点后保留几位 * @return 四舍五入后的结果 */ public static double round(double v, int scale) { if (scale < 0) { throw new IllegalA...
Open Graph Description: 文档里BigDecimalUtil类选择的保留规则是RoundingMode.HALF_UP,即四舍五入。代码部分如下: /** * 提供精确的小数位四舍五入处理。 * * @param v 需要四舍五入的数字 * @param scale 小数点后保留几位 * @return 四舍五入后的结果 */ public static double round(double v, int scal...
X Description: 文档里BigDecimalUtil类选择的保留规则是RoundingMode.HALF_UP,即四舍五入。代码部分如下: /** * 提供精确的小数位四舍五入处理。 * * @param v 需要四舍五入的数字 * @param scale 小数点后保留几位 * @return 四舍五入后的结果 */ public static double round(double v, int scal...
Opengraph URL: https://github.com/Snailclimb/JavaGuide/issues/2129
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"BigDecimal文档里,建议对保留规则设置为ROUND_HALF_EVEN","articleBody":"文档里BigDecimalUtil类选择的保留规则是`RoundingMode.HALF_UP`,即四舍五入。代码部分如下:\r\n```\r\n/**\r\n * 提供精确的小数位四舍五入处理。\r\n *\r\n * @param v 需要四舍五入的数字\r\n * @param scale 小数点后保留几位\r\n * @return 四舍五入后的结果\r\n */\r\n public static double round(double v, int scale) {\r\n if (scale \u003c 0) {\r\n throw new IllegalArgumentException(\r\n \"The scale must be a positive integer or zero\");\r\n }\r\n BigDecimal b = BigDecimal.valueOf(v);\r\n BigDecimal one = new BigDecimal(\"1\");\r\n return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue();\r\n }\r\n```\r\n建议修改为`BigDecimal.ROUND_HALF_EVEN`,也就是`RoundingMode.HALF_EVEN`,即四舍六入五成双(为什么会发现:做项目时发现不同的写法,去查资料看了)。\r\n\r\n我就把百度的四舍六入五成双规则贴过来了:\r\n对于位数很多的近似数,当有效位数确定后,其后面多余的数字应该舍去,只保留有效数字最末一位,这种修约(舍入)规则是“四舍六入五成双”,也即“4舍6入5凑偶”,这里“四”是指≤4 时舍去,\"六\"是指≥6时进上,\"五\"指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲:\r\n(1)5前为奇数,舍5入1;\r\n(2)5前为偶数,舍5不进(0是偶数)。\r\n推荐使用四舍六入五成双规则的原因:\r\n从统计学的角度,“四舍六入五成双”比“四舍五入”要科学,在大量运算时,它使舍入后的结果误差的均值趋于零,而不是像四舍五入那样逢五就入,导致结果偏向大数,使得误差产生积累进而产生系统误差,“四舍六入五成双”使测量结果受到舍入误差的影响降到最低。\r\n\r\n\r\n","author":{"url":"https://github.com/sunquan123","@type":"Person","name":"sunquan123"},"datePublished":"2023-08-10T13:15:37.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/2129/JavaGuide/issues/2129"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:315c88ec-ea8a-e477-038a-08fdcd338bd8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D32E:198E4:3DFEC96:58048F3:6A56AA1A |
| html-safe-nonce | d90ab60a0e9c376d7f3a8611b45cdddd0137be4612c67cb04f6ae2b3db1b2443 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzJFOjE5OEU0OjNERkVDOTY6NTgwNDhGMzo2QTU2QUExQSIsInZpc2l0b3JfaWQiOiI1MTI4NjgwODc1ODMyNTUxOTYyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4ceac2c913b6c37c08e3f08bdb51585c7af62d2c3b6ccf55b6f8856935bf52a5 |
| hovercard-subject-tag | issue:1845170641 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/Snailclimb/JavaGuide/2129/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b4208c8a6a89609a4d5f8e6a7d5918aa623b8968028a821045b80a9a5ed497fa/Snailclimb/JavaGuide/issues/2129 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b4208c8a6a89609a4d5f8e6a7d5918aa623b8968028a821045b80a9a5ed497fa/Snailclimb/JavaGuide/issues/2129 |
| og:image:alt | 文档里BigDecimalUtil类选择的保留规则是RoundingMode.HALF_UP,即四舍五入。代码部分如下: /** * 提供精确的小数位四舍五入处理。 * * @param v 需要四舍五入的数字 * @param scale 小数点后保留几位 * @return 四舍五入后的结果 */ public static double round(double v, int scal... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sunquan123 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 13efe7fe8ecc41eda1078cbde8206041ed92bc54e29bcb737e9353dd9c407aad |
| turbo-cache-control | no-preview |
| go-import | github.com/Snailclimb/JavaGuide git https://github.com/Snailclimb/JavaGuide.git |
| octolytics-dimension-user_id | 29880145 |
| octolytics-dimension-user_login | Snailclimb |
| octolytics-dimension-repository_id | 132464395 |
| octolytics-dimension-repository_nwo | Snailclimb/JavaGuide |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 132464395 |
| octolytics-dimension-repository_network_root_nwo | Snailclimb/JavaGuide |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5466ebe023a0abf876a1e6c5d7602ee8f5e6a02b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width