Created
May 6, 2014 15:38
-
-
Save moorsiek/2e659b51232b5ec4870b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Formats a number or number-like string to a string in number format so that thousands separated with "," and decimal part | |
| * has at least two digits (fractinal part is truncated): e.g. 15321.5636 -> "15,321.56". | |
| * {param} n - A Number type value representing finite number or a String representing the same. If arg doesnt represent finite * number, string "NaN" is returned | |
| * | |
| * returns {String|"NaN"} | |
| */ | |
| var numberToPrice = function(n) { | |
| //efficiency was not the goal | |
| //n - number, d - group of digits of decimal part, f - one or two leading digits of fractional part | |
| return (isNaN(n)||isNaN(n=parseFloat(n)))&&"NaN" || (""+n.toFixed(3)) | |
| .replace(/(-)|(\d{3}|\d{2}|\d)(?=(?:(?:\d{3})+)?(?:\.|$))|\.(\d{1,2})\d*/g, | |
| function(trash, s, d, f){ return s || (d && ',' + d) || ( '.' + (f+"0").slice(0,2) ) }).replace(/^(-?),/, '$1') | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment