I would like to format my numbers to display and replace how many 0's after three 0's
Examples:
number display
------ -------
0.000001 0.0₅1
0.0000003 0.0₆3
0.001 0.001
This is an example how you can achieve this. I have commented the code. Feel free to modify the example yourself if you wish.
const convert = num => {
const subs = [ '₀', '₁', '₂', '₃', '₄', '₅', '₆', '₇', '₈', '₉' ]
const [left, right] = num.toFixed(10).split('.') // break a number by the dot
const result = right.match('^([0]{3,})') // for 000 and more zeroes
if (result){
const length = result[0].length; // get subs size
return(left + '.0' + subs[length] + right.slice(length)) // join all parts
.replace(/0+$/, '') // remove zeroes from the end
}
return num // original
}
console.log([0.000001, 0.0000003, 0.001].map(convert))