Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在函数内部进行数学运算';s参数_Javascript_Typescript - Fatal编程技术网

Javascript 如何在函数内部进行数学运算';s参数

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

我想用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 = 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