查看: 1963|回复: 3

[有事请教] 遇到了取整bug,哪位大佬懂?

[复制链接]
梦石
0
星屑
2866
在线时间
446 小时
注册时间
2016-9-26
回帖
1175
发表于 2021-9-8 11:40:51 | 显示全部楼层 |阅读模式
悬赏100星屑已解决
用脚本试过round、floor和ceil了。
取整一开始很成功。
但是,后面我发现,冒失一个数,比如0.99999,就能取整(冒失吧,我也没法确定),但1.00000001或者2.0000003这种,就没法取整。

最佳答案

查看完整内容

let x=1.00000001; let xy=2.0000000000000003; console.log(x) console.log(xy) console.log(Math.floor(x)) console.log(Math.ceil(xy)) 可以啊不过16位就是最大值了 你可以看到Math.ceil 2.0000000000000003 变成2.0000000000000004 再多一位就变成2 如果你知道想要保留小数的多少位就用toFixed xy.toFixed(5) 2.20000 保留5位小数 保留完再用 round fioor ceil 就可以了 console.log(Math.ceil(xy.toFixed(2))) ...
梦石
0
星屑
2585
在线时间
297 小时
注册时间
2021-5-4
回帖
97
发表于 2021-9-8 11:40:52 | 显示全部楼层
let x=1.00000001;
let xy=2.0000000000000003;
console.log(x)
console.log(xy)
console.log(Math.floor(x))
console.log(Math.ceil(xy))
可以啊不过16位就是最大值了 你可以看到Math.ceil 2.0000000000000003 变成2.0000000000000004 再多一位就变成2
Capture.PNG

如果你知道想要保留小数的多少位就用toFixed
xy.toFixed(5) 2.20000 保留5位小数
保留完再用 round fioor ceil 就可以了
console.log(Math.ceil(xy.toFixed(2)))

点评

this.drawText((Math.floor(newValue4*100000)*0.001).toFixed(1)+"%", x, y+this.lineHeight() *0.83*1, 48, 'right');这样弄了,唔,原来如此  发表于 2021-9-8 14:37
this.resetTextColor();this.drawText(Math.floor(100000*this._actor.xparam(0))*0.001+"%", x + 360, y + lineHeight *0*1, 60, 'right');。好像不顶用,不过,我暂时这样弄了(还在实测)   发表于 2021-9-8 13:57

评分

参与人数 1赞 +1 收起 理由
任小雪 + 1 塞糖

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部