Number Formatting for large integer numbers in Javascript -
i know there in-build function in javascript tolocalstring()
achieve number formatting. question purely learning , logic understanding.
i have function in javascript formats given number in indian number formatting standards (eg: 1,234 | 12,21,123 | etc)
code
function formatter(input) { var inputstr = input.tostring(), l = inputstr.length; var c = 1, f = 0; console.log(l); (var x=l-1; x>=0; x--) { if (x === 0) { continue; } if (c === 3 && f === 0) { inputstr = inputstr.substring(0, x) + ',' + inputstr.substring(x); f = 1; c = 0; } else if (c % 2 === 0 && f === 1) { inputstr = inputstr.substring(0, x) + ',' + inputstr.substring(x); c = 0; } c++; } return inputstr; }
now works part (as far have test, point out bugs if spot any). question how handle large number in this, i.e. how handle values greater 9007199254740991.
Comments
Post a Comment