Javascript 如何在函数内部进行数学运算';s参数
我想用0.74乘以传入的API价格。在下面的第一个参数中,我想做一个数学运算 当前获取类型脚本错误Javascript 如何在函数内部进行数学运算';s参数,javascript,typescript,Javascript,Typescript,我想用0.74乘以传入的API价格。在下面的第一个参数中,我想做一个数学运算 当前获取类型脚本错误当我尝试执行下面的数学运算时,算术运算的左侧必须是“any”、“number”、“bigint”类型或枚举类型 // If current price is equal to USD, make the Canadian price appear instead. if (curProduct.price?.currency !== "CAD") { curPrice = f
当我尝试执行下面的数学运算时,算术运算的左侧必须是“any”、“number”、“bigint”类型或枚举类型
// If current price is equal to USD, make the Canadian price appear instead.
if (curProduct.price?.currency !== "CAD") {
curPrice = formatPrice(
curProduct.price.text * 0.74, // error is here, also want to do math operation here.
this.props.lang,
true
);
}
我假设curProduct.price.text
是一个字符串,如关键字text
所示
您可以通过预先设置+
将字符串解释为数字
// If current price is equal to USD, make the Canadian price appear instead.
if (curProduct.price?.currency !== "CAD") {
curPrice = formatPrice(
+(curProduct.price.text) * 0.74,
this.props.lang,
true
);
}
大概curProduct.price.text
是文本,而不是数字。文本不能乘以0.74。使用parseInt
将此文本转换为int,或者使用curProduct.price
的另一个字段,该字段已经是一个数字。谢谢!另一个错误是“Object可能是‘null’”,但至少我正在进步。我建议你去找一个像样的基本javascript教程,学习它,直到你真正理解它为止。编程逻辑(尽管人们普遍认为)实际上需要。。。等等。。。思考教育将为您提供思考编程所需的信息,而不是猜测。@gforce301谢谢您的提示,巴德!:)谢谢你的回答!尝试过之后,VS代码在保存时自动删除curProduct.price.text周围的括号。现在得到一个错误,并在该段代码上说,“对象可能是‘null’”@好吧,我就是忍不住。什么是“VS代码”?这些不是“括号”,它们被称为括号。@gforce301 Visual Studio Code=一个轻量级的免费IDE