While working with locale translation and parsing decimal numbers from text to numerical values in Angular 10, I came across the following problem:
Consider the string value value = "-35.17 %"
. I want to convert this a numerical value using parseFloat(value)
. This works fine for application locale en-US.
However, if the user changes application locale to nb-NO (Norwegian), the parsing fails, resulting in a NaN
.
The reason for this is that the Norwegian locale uses a different character for the negative prefix (? instead of -).
The workaround for this particular issue is simple, by performing a .replace("?", "-")
on the string before parsing, but shouldn't JavaScript be able to handle parsing of both these characters? Is it only safe to perform parsing on locale en-US?
question from:
https://stackoverflow.com/questions/65601940/javascript-function-parsefloat-fails-for-negative-values-in-certain-locales 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…