如何将一个数字的值增加到10、100、1000、10000的下一个倍数,依此类推
How to increase the value of a number to the next multiple of 10, 100, 1000, 10,000 and so on
你必须原谅这个问题的措辞,我相信有更好、更简洁的提问方式,但我不知道。
假设我有一张图,所有y轴的值都是
[0,4,5,3,2,5,6]
最大值为6。因此,我希望Y比例标记为从0到10。
给定以下值
[33,26,54,23,86,23]
最大值是86,所以我希望Y比例从0到90。
现在假设我有以下值
[98,253,87, 876,263]
最大值为876,因此Y刻度应该从0到900
现在,我已经创建了以下函数,该函数将为我提供迄今为止所需的所有最大y刻度值。
function padMaxValue(value){
for(var i = 1; i < 1000000000000000000; i = i * 10){
var decimalValue = value / i;
if(value === i){
return i;
}
if(decimalValue < 1 && decimalValue > 0.09){
return i;
}
}
}
然而,给定以下值
[9913,82189,45]
我的函数将y刻度最大值设置为1000
。但最大值应该是200。我意识到我真正需要的是一种更聪明的方法来增加i
的值,而不仅仅是将其乘以10。我需要能够将I的值增加10,一直增加到100。然后增加100,一直增加到1000。然后增加1000,一直增加到10000,以此类推
我觉得应该有一些简洁的数学方法来做这件事。我还觉得我在for循环中的1000000000000000000
数字暴露了我对数学的无知。
不管怎样,这就是问题所在。有什么想法吗?
没有必要进入字符串的领域,如果有十进制值,这可能会很尴尬。
function RoundedMax(a) {
var mx = Math.max.apply(Math, a);
if (mx == 0) {return 0};
var size = Math.floor(Math.log(Math.abs(mx)) / Math.LN10);
var magnitude = Math.pow(10, size);
var yMax = Math.ceil(mx / magnitude) * magnitude;
return yMax;
}
function RoundedMin(a) {
var mn = Math.min.apply(Math, a);
if (mn == 0) {return 0};
var size = Math.floor(Math.log(Math.abs(mn)) / Math.LN10);
var magnitude = Math.pow(10, size);
var yMin = Math.floor(mn / magnitude) * magnitude;
return yMin;
}
var arr = [-9.9,-1.23,-8.2,-2.01,-4.5,0];
document.write(RoundedMax(arr) + " " + RoundedMin(arr));
输出:0 -10
。
编辑根据评论更新。现在甚至适用于IE8。
现在(2023),所有当前的浏览器都支持ECMAScript6:
function RoundedMax(a) {
var mx = Math.max.apply(Math, a);
if (mx == 0) {return 0};
var size = Math.floor(Math.log10(Math.abs(mx)));
var magnitude = Math.pow(10, size);
var yMax = Math.ceil(mx / magnitude) * magnitude;
return yMax;
}
function RoundedMin(a) {
var mn = Math.min.apply(Math, a);
if (mn == 0) {return 0};
var size = Math.floor(Math.log10(Math.abs(mn)));
var magnitude = Math.pow(10, size);
var yMin = Math.floor(mn / magnitude) * magnitude;
return yMin;
}
我不喜欢数学,所以这里有一个非常简单/有趣的字符串操作解决方案:
查找最大值:
var max = Math.max.apply(Math, [98,253,87, 876,263]); // 876
取其第一个字符
var c = max.toString()[0] // "8"
将其设为整数并添加1
c = (c | 0) + 1 // 9
将其转换回字符串:
c = c.toString() // "9"
加上N-1个零,其中N是原始号码的长度:
c += Array(max.toString().length).join("0") // "900"
将其转换回整数:
c = (c | 0) // 900
完成!
不过说真的,用数学。
我相信这会对你有用:
var data = [98, 253, 87, 876, 263, -155];
var max = Math.max.apply(null, data); //
var factor = Math.pow(10, Math.floor(Math.log(Math.abs(max)) / Math.LN10));
if (factor == 0) { factor = 1; }
var magnitude = Math.ceil(max / (factor * 1.00)) * factor;
基本上,上面发生的事情如下:
- 查找样本的最大值
- 获取最大值length,然后将其提高到10的幂,即345,length=3,因此因子为100
- 确保我们不除以0
- 将最大数字除以因子得到一个小数,取该数字的上限,然后将其乘以因子得到正确的0
更新:如果您想找到最小值(对于负值),只需将Math.ceil
翻转到Math.floor
即可。您还必须取最小值的绝对值,以确保不会将负数字符作为字符串的一部分。
var data = [98, 253, 87, 876, 263, -155];
var min = Math.min.apply(null, data);
var factor = Math.pow(10, Math.floor(Math.log(Math.abs(max)) / Math.LN10));
if (factor == 0) { factor = 1; }
var mag = Math.floor(min / (factor * 1.00)) * factor // mag = -200;
更新2:正如许多人在评论中所说,我们不应该使用字符串操作。我更新了代码,改为使用对数。
我最终得到了:
function getGraphRange(data)
{
var max=Math.max.apply(null, data);
var min=Math.min.apply(null, data);
var maxDigits=max.toString().length;
var minDigits=min.toString().length;
var maxD=Math.pow(10,Math.max((maxDigits-1),1));
var minD=Math.pow(10,Math.max((minDigits-1),1));
var maxR=(Math.ceil(max/maxD)*maxD);
var minR=(Math.floor(min/minD)*minD);
return [minR,maxR];
}
alert(getGraphRange([11, 20, 345, 99]).join(' - '));//10-400
alert(getGraphRange([0,4,5,3,2,5,6]).join(' - '));//0-10
alert(getGraphRange([98,253,87,876,263]).join(' - '));//80-900
http://jsfiddle.net/p4xjs9na/
这里已经有了很好的答案。我用while循环来解决这个问题。该函数非常快速,即使是非常大的数字,它也可以在不到一秒钟的时间内返回值。
function padMaxValue(value)
{
var i = 10;
var counter = 0;
var roundMax = 10;
var copyValue = value;
//If the value is negative, convert copyValue to positive
if(value < 0) copyValue *= -1;
while(roundMax <= copyValue)
{
roundMax += i;
counter++;
if(counter == 9)
{
i *= 10;
counter = 0;
}
}
if(value < 0) roundMax *= -1;
return roundMax;
}
document.write(padMaxValue(8) + "<br>");
document.write(padMaxValue(77) + "<br>");
document.write(padMaxValue(889.65) + "<br>");
document.write(padMaxValue(-880.65) + "<br>");
document.write(padMaxValue(-765889.65) + "<br>");
document.write(padMaxValue(7888.88) + "<br>");
document.write(padMaxValue(88999887788899.099887) + "<br>");
输出:
10
80
900
-900
-800000
8000
90000000000000
我不太喜欢数学,但我认真对待编程!我想出了这个解决方案!
function getMax(max){
var mCopy = max;
var d=1;//Assuming everything is greater than 10,
while(Math.floor(mCopy) > 10){//if the number is not one digited
mCopy /= Math.pow(10,d);
d++;
}
d--;//back one digit
if(d<1)
d++;
if((Math.floor(max / Math.pow(10,d))*Math.pow(10,d))==max)
return max;
return Math.floor(max / Math.pow(10,d))*Math.pow(10,d) + Math.pow(10,d);
}
希望这能帮助
在我看来,给出的答案过于复杂。下面是一个可以在MATLAB中使用的匿名函数:
next = @(x,n) ceil(x/n)*n;
>> next(11,10)
ans =
20 %# 20 is the next "10" after "11")
>> next(110,100)
ans =
200 %# 200 is the next "100" after "110"
>> next([98,253,87, 876,263],100)
ans =
100 300 100 900 300
x
是应该处理的数字,n
是请求的步长。
编辑自动确定下一个数量级也是可能的
>> nextOrder = @(x) ceil(x/10.^ceil(log10(x))) * 10.^ceil(log10(x));
>> nextOrder([98,253,87,876,263])
ans =
100 1000 100 1000 1000
- Javascript图形可视化工具包,具有高性能(500-1000个节点)
- 创建一个javascript数组(N),该数组由连续的较小整数组成,其和为1000
- 是什么让新的Date() / 1000成为一个有效的javascript
- D3/Raphael js 以慢帧率绘制 1000+ 动画圆圈
- 如何从包含1000个项目的数组中随机选择一个项目
- Performance: toFixed() vs. Math.floor(x * 10000)
- 从web服务中检索10000行并插入到OWC11电子表格中
- JavaScript:计算斐波那契序列值中所有偶数的总和< 10000
- JavaScript 数组中元素的最大数量.浏览器不允许超过 1000 个元素
- jquery选择器在1000+元素与450Mhz机器时变慢
- Blueimp 上传插件使用 1000 字节作为单个 kb 而不是 1024
- 1000 mp3 歌曲使 HTML5 音频标签滞后
- 使用 $.ajax 从服务器加载 1000+ 条记录时出错
- 仅当屏幕上显示 <1000 像素时调用 JS 函数
- Firefox 在这个简单的枚举上抛出安全错误 1000
- 如何格式化干净的数字,使 1000 显示为 1.000,00
- 如何有效地加载1000 +谷歌地球标记
- 如何将一个数字的值增加到10、100、1000、10000的下一个倍数,依此类推
- add_comma函数不处理10000,但处理1000
- (int)10000/1000在php中做什么?