The Numeral.js JavaScript library assists us with getting the required formats for the numbers our program uses.

The following code sample shows how simple it is to use this library in order to manipulate the format of the numeric values we use in our code.

<!DOCTYPE html>
<html>
<head>
    <title>numeral.js demo</title>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/numeral.js/1.4.5/numeral.min.js">
    </script>
</head>
<body>
<script type="text/javascript">
document.write("<h4>format samples</h4>");
var str;
//numbers
str = numeral(123002132.23423).format('0,0');
document.write("str="+str);
//currency
str = numeral(123002132.23423).format('$0,0.00');
document.write("<br/>str="+str);
//bytes
str = numeral(123002132.23423).format('0b');
document.write("<br/>str="+str);
//percentages
str = numeral(1.25).format('0%');
document.write("<br/>str="+str);
//time
str = numeral(214).format('00:00:00');
document.write("<br/>str="+str);
document.write("<h4>unformat samples</h4>");
str = numeral().unformat('124k');
document.write("str="+str);
str = numeral().unformat('($800,000)');
document.write("<br/>str="+str);
</script>
</body>
</html>

The following video clip overviews this code sample, explains each and every part of it and shows its execution.

You can find more video clips, code samples and training material for learning this library in my free online courses website at http://abelski.lifemichael.com.

Leave a Reply

Your email address will not be published. Required fields are marked *