Java自定义双重圆形

发布于 2025-02-01 16:14:40 字数 204 浏览 3 评论 0原文

我需要进行以这种方式工作的自定义舍入:

  • 如果数字接近0,则将其舍入(1或2)
  • 如果数字接近0.5,则将其四舍五入为0.5(3、4、6或7),
  • 如果数字接近1,它被四舍五入(8或9)

是解决此问题的最佳方法?

编辑:这个问题已经在“ Java Round到最接近的.5”中回答。感谢您的答案。

I need to do a custom rounding that works that way:

  • If number is closer to 0, it is rounded down (1 or 2)
  • If number is closer to 0.5, it is rounded to 0.5 (3, 4, 6 or 7)
  • If number is closer to 1, it is rounded up (8 or 9)

What would be the best approach to this problem?

Edit: this question was already answered in "Java round to nearest .5". Thanks for the answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

極樂鬼 2025-02-08 16:14:41

我认为您正在查看0.0至1.0范围内的数字。您可以使用所提供的范围边界。如果num< 0.3返回0.0,或者如果num< 0.8退货0.5,否则返回1.0

I assume that you are looking at numbers in the range 0.0 to 1.0. You could work with the range boundaries you give. If num < 0.3 return 0.0, or if num < 0.8 return 0.5, otherwise return 1.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文