如何自定义舍入的形式 [英] How to customize the form of rounding

查看:100
本文介绍了如何自定义舍入的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题可能看起来很简单,但仍然无法获得有效的东西。
我需要自定义Math.round舍入格式或其他内容以使其工作如下:

My question may seem simple, but still can not get something that works. I need to customize the Math.round rounding format or something to make it work as follows:

如果数字是1.6,他应该舍入为1,如果大于或等于1.7应该舍入到2.0。所以使用#。6
对所有其他小数结果我将1.6舍入为2的方式应舍入为1.

If the number is 1.6 he should round to 1, if greater than or equal to 1.7 should round to 2.0 . And so to all other decimal results with # .6 The way I'm doing the 1.6 being rounded to 2 shall be rounded to 1.

怎么能我这样做了吗?

谢谢!

推荐答案

只需这样做:

double threshold = 0.7;
Math.round(x - threshold + 0.5);

这篇关于如何自定义舍入的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆