四舍五入到最接近的10的幂 [英] Round up to nearest power of 10

查看:86
本文介绍了四舍五入到最接近的10的幂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何将数字(大于0)四舍五入到最接近的10的幂.

I'm trying to figure out how to round up a number (greater than 0) to the nearest power of 10.

示例:

roundUp(23.4) = 100
roundUp(2.34) = 10
roundUp(.234) = 1
roundUp(0.0234) = 0.1
roundUp(0.00234) = 0.01

对于大于1的数字,我认为这可行:

For numbers greater than 1, I believe this works:

10^(ceil(log10(x)))

但是对于介于0和1之间的数字,我不确定如何得出答案.

But for numbers between 0 and 1, I'm not sure how to arrive at the answer.

推荐答案

糟糕.我没有意识到该功能实际上适用于0到0之间的数字. 1.真是令人讨厌,我看到log10(x)为负数,只是假设我不能接受其中的10 ^.

Oops. I didn't realize the function actually DOES work for numbers between 0 & 1. It was a brain fart that I saw a negative number for log10(x) and just assumed I couldn't take 10^ of that.

继续前进.

这篇关于四舍五入到最接近的10的幂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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