let money = isNaN(number) ? 0.0 : parseFloat((number / 100.0).toFixed(2));
时间: 2024-05-30 11:10:24 浏览: 14
这段代码是一个简单的三目运算符,它的作用是将一个输入的数字除以100并保留两位小数,然后将结果赋给变量money。如果输入的不是数字,则将money的值设置为0.0。
更具体地说,isNaN()函数用来判断参数是否为NaN(Not a Number,非数值),如果是则返回true,否则返回false。三目运算符的第一个条件就是判断输入的number是否为NaN,如果是则money的值为0.0,否则通过parseFloat()函数将number转换为浮点数,并使用toFixed()方法保留两位小数。最后将结果赋给变量money。
相关问题
this.sampleDate[0].gross= res.count3; this.sampleDate[0].process = res.count1+res.count2; let result1:Number = parseFloat(((this.sampleDate[0].process / this.sampleDate[0].gross).toFixed(2))); if (typeof result1 === 'number' && !isNaN(result1)) { this.sampleDate[0].percentage = result1 * 100; } else { this.sampleDate[0].percentage = 0; } this.sampleDate[1].gross = 12; this.sampleDate[1].process = 4; let result2:Number = parseFloat(((this.sampleDate[1].process / this.sampleDate[1].gross).toFixed(2))); if (typeof result2 === 'number' && !isNaN(result2)) { this.sampleDate[1].percentage = result2 * 100; } else { this.sampleDate[1].percentage = 0; }
可以简化为以下代码:
```
const updateSampleDate = (sampleDate: any[], gross: number, process: number, index: number): void => {
sampleDate[index].gross = gross;
sampleDate[index].process = process;
const percentage = parseFloat(((process / gross) * 100).toFixed(2));
sampleDate[index].percentage = isNaN(percentage) ? 0 : percentage;
};
updateSampleDate(this.sampleDate, res.count3, res.count1 + res.count2, 0);
updateSampleDate(this.sampleDate, 12, 4, 1);
```
这里使用了一个 `updateSampleDate` 函数来更新 `sampleDate` 数组中的数据。这个函数接收三个参数:`sampleDate` 数组、总量 `gross`、处理量 `process` 和索引 `index`。在函数内部,先通过传入的参数更新 `sampleDate` 数组中对应索引的 `gross` 和 `process` 值,然后计算出百分比并更新到 `percentage` 属性中。如果计算结果是 `NaN`,则将 `percentage` 属性设置为 0。最后,通过调用 `updateSampleDate` 函数更新两个数据项即可。
if math.isnan(tem[i]) == True: TypeError: must be real number, not str
这个错误是因为你在使用 `math.isnan()` 函数时传入的参数不是一个实数类型,而是一个字符串类型。`math.isnan()` 函数用于判断一个数是否为 NaN(非数字),它只能接受实数类型的参数。
要解决这个问题,你需要确保传入 `math.isnan()` 函数的参数是一个实数类型。你可以使用 `float()` 函数将字符串转换为实数类型,例如:
```
tem = ["1.2", "2.3", "NaN"]
for i in range(len(tem)):
if tem[i].lower() == "nan":
tem[i] = float("nan")
else:
tem[i] = float(tem[i])
if math.isnan(tem[i]) == True:
print("tem[{}] is NaN".format(i))
```
在这个例子中,我们使用 `float()` 函数将字符串类型的 "NaN" 转换为实数类型的 NaN,然后再进行判断。注意,在判断 "NaN" 时,我们使用了 `lower()` 方法将字符串转换为小写字母,这是为了避免大小写带来的问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)