所有其他解决方案都需要您预先知道格式。在每种情况下,我需要检测(!)格式,这就是我最终得到的结果。
function detectFloat(source) {
let float = accounting.unformat(source);
let posComma = source.indexOf(',');
if (posComma > -1) {
let posDot = source.indexOf('.');
if (posDot > -1 && posComma > posDot) {
let germanFloat = accounting.unformat(source, ',');
if (Math.abs(germanFloat) > Math.abs(float)) {
float = germanFloat;
}
} else {
float = accounting.unformat(source, ',');
}
}
return float;
}
这是针对以下情况进行的测试:
const cases = {
"0": 0,
"10.12": 10.12,
"222.20": 222.20,
"-222.20": -222.20,
"+222,20": 222.20,
"-222,20": -222.20,
"-2.222,20": -2222.20,
"-11.111,20": -11111.20,
};
欢迎提出建议。
replace
都应该改为replaceAll
! - Sebastian Simon