Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

number_format in jquery

function number_format (number, decimals, dec_point, thousands_sep) {
    // Strip all characters but numerical ones.
    number = (number + '').replace(/[^0-9+-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/B(?=(?:d{3})+(?!d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

number_format('1486.10',2,'.',',') // return 1,486.10
Comment

number_format in jquery

function number_format (number, decimals, dec_point, thousands_sep) {
    // Strip all characters but numerical ones.
    number = (number + '').replace(/[^0-9+-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/B(?=(?:d{3})+(?!d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript inject html 
Javascript :: capacitor.ionicframework.com to apk 
Javascript :: js list pf objects 
Javascript :: how to flip a Number in javascript 
Javascript :: javascript set time to start of day 12 am 
Javascript :: axios post form data and json 
Javascript :: round decimal js 
Javascript :: javascript anagram two strings 
Javascript :: node js return ID in postgres insert 
Javascript :: string concatenation javascript 
Javascript :: js how to round up 2 decimal places 
Javascript :: import bootstrap in react 
Javascript :: how to export a variable in javascript 
Javascript :: js notifications 
Javascript :: array.unshift in javascript 
Javascript :: string to json nodejs 
Javascript :: ctx.fillstyle 
Javascript :: how to find id in array javascript 
Javascript :: make copy of date javascript 
Javascript :: how to break the foreach loop in javascript 
Javascript :: jquery set select value 
Javascript :: add table row jquery 
Javascript :: date regex format 
Javascript :: @babel/plugin-proposal-optional-chaining 
Javascript :: react-native navigation screen props 
Javascript :: preloader js code 
Javascript :: jquery get dropdown list selected value 
Javascript :: stream recording javascript 
Javascript :: nodejs routes 
Javascript :: javascript how to select radio button 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =