舍入变量

时间:2018-02-22 13:20:14

标签: javascript variables rounding

在这个脚本中,我想在将自身的30%添加到自身之后对Cost6MP进行舍入,这样就不会看到小数。他们是否可以绕过这个变量(最好是向下)?

<script type="text/javascript">
function gainCoinsPS6MP() {
if (Bitcoins > Cost6MP - 1) {
    BitcoinsPS = BitcoinsPS + 1;
    Bitcoins = Bitcoins - Cost6MP;
    Amount6MP = Amount6MP + 1;
    PS6MP = PS6MP + 1;
    Cost6MP = Cost6MP + ((Cost6MP * 30)/100)

} else {
  alert("Are you trying to scam me!?");
}
}
</script>

1 个答案:

答案 0 :(得分:1)

尝试更改最后一行以使用Math.floor,它就像一个向下舍入的方法:

Cost6MP = Math.floor(Cost6MP + ((Cost6MP * 30)/100))

正如文档所示,这是一些示例效果:

console.log(Math.floor(5.95));
// expected output: 5

console.log(Math.floor(5.05));
// expected output: 5

console.log(Math.floor(5));
// expected output: 5

console.log(Math.floor(-5.05));
// expected output: -6