When I try to use Excel to update products, I encounter an error related to the presence of NaN
. Here is the specific error message:
CastError: Cast to Number failed for value "NaN" (type number) at path "markupPercentage"
messageFormat: undefined,
stringValue: '"NaN"',
kind: 'Number',
value: NaN,
path: 'markupPercentage',
reason: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert.ok(!isNaN(val))
Function
export function roundNumber(value: any, numberOfDecimals: number = 2): number {
if (value) {
if (!isNaN(value)) {
switch (numberOfDecimals) {
case 0:
return Math.round(value * 1) / 1
case 1:
return Math.round(value * 10) / 10
case 2:
return Math.round(value * 100) / 100
case 3:
return Math.round(value * 1000) / 1000
case 4:
return Math.round(value * 10000) / 10000
case 5:
return Math.round(value * 100000) / 100000
case 6:
return Math.round(value * 1000000) / 1000000
default:
return Math.round(value * 100) / 100
}
} else {
return parseFloat(value.toFixed(numberOfDecimals))
}
} else {
if (value === 0) {
return 0
}
}
}
Code
case 'markupPercentage':
article.markupPercentage += percentage
article.markupPercentage = roundNumber(article.markupPercentage, decimal)
article.markupPrice = roundNumber((article.costPrice * article.markupPercentage) / 100)
article.salePrice = article.costPrice + article.markupPrice
break
Model
markupPercentage: {type: Number, default: 0},