Skip to content Skip to sidebar Skip to footer

Jquery Function To To Format Number With Commas And Decimal

I'm using the following function to format numbers as the user types. It will insert a comma every 3 numbers. Ex: 45696.36 becomes 45,696.36. However, I've run into a problem with

Solution 1:

You could accomplish this by splitting your string at the '.' character and then performing your comma-conversion on the first section only, as such:

functionReplaceNumberWithCommas(yourNumber) {
    //Seperates the components of the numbervar n= yourNumber.toString().split(".");
    //Comma-fies the first part
    n[0] = n[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    //Combines the two sectionsreturn n.join(".");
}

ReplaceNumberWithCommas(1136.6696); //yields 1,136.6696

Example

Solution 2:

Post a Comment for "Jquery Function To To Format Number With Commas And Decimal"