Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

number to money javascript

/**
 * Number.prototype.format(n, x, s, c)
 * 
 * @param integer n: length of decimal
 * @param integer x: length of whole part
 * @param mixed   s: sections delimiter
 * @param mixed   c: decimal delimiter
 */
Number.prototype.format = function(n, x, s, c) {
    var re = 'd(?=(d{' + (x || 3) + '})+' + (n > 0 ? 'D' : '$') + ')',
        num = this.toFixed(Math.max(0, ~~n));

    return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
};

12345678.9.format(2, 3, '.', ',');  // "12.345.678,90"
123456.789.format(4, 4, ' ', ':');  // "12 3456:7890"
12345678.9.format(0, 3, '-');       // "12-345-679"
Comment

number to money javascript

/**
 * Number.prototype.format(n, x)
 * 
 * @param integer n: length of decimal
 * @param integer x: length of sections
 */
Number.prototype.format = function(n, x) {
    var re = 'd(?=(d{' + (x || 3) + '})+' + (n > 0 ? '.' : '$') + ')';
    return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&,');
};

1234..format();           // "1,234"
12345..format(2);         // "12,345.00"
123456.7.format(3, 2);    // "12,34,56.700"
123456.789.format(2, 4);  // "12,3456.79"
Comment

PREVIOUS NEXT
Code Example
Javascript :: canvas change line color 
Javascript :: float js precision 
Javascript :: gradle json simple dependency 
Javascript :: react-native shadow generator 
Javascript :: how to get current year in javascript 
Javascript :: react native cli run ios 
Javascript :: jquery wysiwyg editor val acf 
Javascript :: saturn range in angular display end date 
Javascript :: unpacking array javascript 
Javascript :: messageReactionAdd 
Javascript :: jquery give control focus 
Javascript :: js remove quotes from string 
Javascript :: js loop through object 
Javascript :: change value rateit.js using jquery 
Javascript :: js convert string to script 
Javascript :: delete duplicates array of strings Javascript 
Javascript :: windows 10 toast notifications nodejs 
Javascript :: console.log red text on yellow background 
Javascript :: hover vanilla javascript 
Javascript :: javascript add class to all child elements 
Javascript :: model schema mongoose 
Javascript :: angular version command 
Javascript :: jetbrains mono 
Javascript :: japan 
Javascript :: hiding header in a specific screen in react native 
Javascript :: load js file 
Javascript :: jQuery delete all spans in a div 
Javascript :: js array for in vs for of 
Javascript :: framer motion styled components 
Javascript :: jshint es6 vscode 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =