I have four number formats '123 456,78', '123.456,78', '123'456.78', '123,456.78' in my application with dynamic decimal point. Is there any library or method in Javascript to Format number and remove format method?
2 Answers
You can use Numeral.js
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/1.4.5/numeral.min.js"></script>
var string = numeral(1000).format('0,0');
// '1,000'
Comments
You have several functions on the Number class that give you formatting of numbers:
var n = 5.1234567;
var sf = n.toFixed();
var se = n.toExponential();
var sp = n.toPrecision();
You can find details about these functions here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision
More or less, these are equivalent to the %f and %g in C/C++ printf() format with just a size representing the number of digits to display.