如何四舍五入到最接近的 10 次方? [英] How to round down to the nearest power of 10?
本文介绍了如何四舍五入到最接近的 10 次方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我什至找不到这方面的搜索关键字.请考虑以下代码:
I can't even find search keywords for this. Please consider this code:
float inputValue = getInputValue();
float resultValue;
if (inputValue < 0.1f) {
resultValue = 0.01f;
}
else if (inputValue < 1.0f) {
resultValue = 0.1f;
}
else if (inputValue < 10.0f) {
resultValue = 1.0f;
}
else {
resultValue = 10.0f;
}
等等.必须有一种更优雅的方式来做到这一点.我想解决方案很简单,但我现在尝试找到一种方法 2 小时并阅读关于圆形、天花板、地板...找不到它.
and so on. There must be a more elegant way to do this. I guess the solution is easy, but I try to find a way now for 2 hours and read about round, ceil, floor...can't find it.
有人有想法吗?
推荐答案
powf(10.0f, floorf(log10f(value)))
这篇关于如何四舍五入到最接近的 10 次方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文