我正在使用 React-Native 构建一个费用计算器,并且我有多个输入字段,用于设置计算变量。
当输入字段具有正确的值时,一切正常,但是如果用户在之前输入的内容上按退格键,然后尝试使用空字段计算利润,则总数将显示为 NaN。
到目前为止,这是我的代码:
this.state = {
text: '',
soldPrice: 0,
shippingCost: 0,
shippingCharge: 0,
itemCost: 0,
profit: 0,
paypalFee: 0.30,
paypalFeePercentage: 0.029,
ebayFee: 0.1
};
}
calculateProfit = () => {
const { soldPrice, shippingCost, shippingCharge, itemCost,
paypalFee, ebayFee, paypalFeePercentage
} = this.state;
let sp = parseFloat(soldPrice);
const sc = parseFloat(shippingCost);
const sCharge = parseFloat(shippingCharge);
const ic = parseFloat(itemCost);
const pf = parseFloat(paypalFee);
const ef = parseFloat(ebayFee);
const pfp = parseFloat(paypalFeePercentage);
// sp = soldPrice, sc = …Run Code Online (Sandbox Code Playgroud)