检查值(value)是否为一便士或以下的最佳方法是什么。如果低于则返回 0,否则返回 Math.floored 值。
如果 Price_usd 类似于 00.0031
,下面的代码当前将返回 0.01 美元的值。应该返回的是0
。
但是,如果该值为 00.56
,则应返回 0.56 美分,现在就是这样。
const rounder = (balance, price_usd) => round(multiply(balance, price_usd));
constructor(props) {
super(props)
this.state = {
asset: props.asset,
balance: props.balance,
value: rounder(props.balance, props.price_usd)
};
this.handleChange = this.handleChange.bind(this);
}
// value: rounder(props.balance, Math.floor(props.price_usd))
最佳答案
你可以使用这个技巧来做到这一点。
var num1 = 0.56,
num2 = 0.0031;
console.log(Math.floor(num1 * 100) / 100); // 0.56
console.log(Math.floor(num2 * 100) / 100); // 0.00
关于javascript - 不到一分钱怎么铺地板,否则不要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44726561/