Java自定义双重圆形
我需要进行以这种方式工作的自定义舍入:
- 如果数字接近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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在查看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