我怎样才能获得的5或10的下一个最高倍数 [英] How can i get the next highest multiple of 5 or 10
本文介绍了我怎样才能获得的5或10的下一个最高倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个给定的双
我要根据一定的规则当中,因为我有一定的难度,描述他们,我会通过例子说明,以获得下一个最高数量:
输入所需的输出
------- --------------
0.08 0.1
0.2 0.5
5 10
7 10
99 100
100 500
2345 5000
输出应该在一定意义上的5或10次高的多。
我希望这是可以理解的;如果没有,让我知道。
的实施将是在Java和投入将是积极的双
秒。
解决方案
函数top5_10(X){
变种10 = Math.pow(10,Math.ceiling(Math.ln(x)的/Math.LN10)));
如果(10→10 * x)的{10 = 10/10; }
否则,如果(十大< = X){10 = 10 * 10; }
返回X< 10/2? 10分/ 2:十;
}
或者是这样的: - )
From a given double
I want to get the next highest number according to some rules which, since I have some difficulty describing them, I will illustrate by examples:
Input Desired output
------- --------------
0.08 0.1
0.2 0.5
5 10
7 10
99 100
100 500
2345 5000
The output should be in some sense the 'next highest multiple of 5 or 10'.
I hope this is understandable; if not, let me know.
The implementation will be in java and input will be positive double
s.
解决方案
function top5_10 (x) {
var ten = Math.pow(10, Math.ceiling(Math.ln(x)/Math.LN10)));
if (ten > 10 * x) { ten = ten / 10; }
else if (ten <= x) { ten = 10 * ten; }
return x < ten / 2 ? ten / 2 : ten;
}
or something like this :-)
这篇关于我怎样才能获得的5或10的下一个最高倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文