So let's say I have a string like this:
'6,18284828814828481'
Javascript Number do support a certain amount of decimals. from what I see I should return something like this:
6.182848288148285
Is there a way I can convert the string to the closest valid number?
What I try to do is something like this:
const limitDecimals = (num: string): string|number => {
let maxDecimalDigits = 17;
for(let i = maxDecimalDigits; i >= 0; i--) {
let [integerPart, decimalPart] = num.split('.');
if(decimalPart){
decimalPart = decimalPart.slice(0, i);
if(decimalPart.length > 0) {
return`${integerPart}.${decimalPart}`;
}
}else{
return integerPart;
}
}
return num;
}
Note: this is not converted in Number since it would not work yet