10000个怎么用js写 创建li_如何将数字的值增加到10, 100, 1000、10000等的下一个倍数...

gargantuan提出了一个问题:How to increase the value of a number to the next multiple of 10, 100, 1000, 10,000 and so on,或许与您遇到的问题类似。

There is no need to go into the land of strings, which could be awkward if you ever had a decimal value.

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));

Outputs: 0 -10.

EDIT Updated in view of the comments. Now works even in IE8.

希望本文对你有帮助,欢迎支持JavaScript中文网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值