Add comma separator to a numbers with 2 decimal points

rory-h

I've managed to make the user input to 2 decimal points: enter image description here Below is my code for two input fields:

$('.ave-daily-accbalance').blur(function(){
    var num = parseFloat($(this).val()) || 0;
    var cleanNum = num.toFixed(2);
    $(this).val(cleanNum);

    });

    $('.facilitylimit').blur(function(){
    var num = parseFloat($(this).val()) || 0;
    var cleanNum = num.toFixed(2);
    $(this).val(cleanNum);

    });

But now I want to seperate the input with comma. So if the user inputs in 500000 - it automatically converts to 500,000.00

Jeffrey Z

You can use Number.prototype.toLocaleString() (documentation) In your case you want to use the second options parameter to specify the number of decimal points you want:

var num = 200000.001231;
var cleanNum = num.toLocaleString('en', {minimumFractionDigits: 2, maximumFractionDigits: 2});
console.log(cleanNum);

Note that the options argument does not work on all browser and versions. If compatibility is key, use num.toLocaleString() with no arguments, and trim/append decimals as needed:

var num = 20000.001231;
var cleanNum = num.toLocaleString('en');
var splitNum = cleanNum.split('.');
if (splitNum.length < 2) {
  // Need to append .00 if num was an integer
  cleanNum = cleanNum + '.00';
} else {
  // Append 0 if there was only 1 decimal, otherwise trim
  // to 2 decimals
  var decimals = splitNum[1];
  decimals = decimals.length < 2 ? decimals + '0' : decimals.slice(0, 2);
  cleanNum = splitNum[0] + '.' + decimals;
}
console.log(cleanNum);

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Parsing numbers with a comma decimal separator in JavaScript

From Dev

Google form regex for numbers with comma as decimal separator

From Dev

Only add thousand separator before decimal comma

From Dev

Pasting decimal numbers in excel / comma and point decimal separator

From Dev

Convert a double to 2 decimal places with comma separator

From Dev

Convert a double to 2 decimal places with comma separator

From Dev

Masked EditText with comma as decimal separator

From Dev

seq uses comma as decimal separator

From Java

Pandas convert numbers with a comma instead of the point for the decimal separator from objects to numbers

From Dev

Format a decimal with comma as decimal separator and without thousands separator

From Dev

Getting numbers behind decimal separator

From Dev

Add comma separator to axis labels

From Dev

Jaxb conversion of number with comma as a decimal separator

From Dev

Why django uses a comma as decimal separator

From Dev

How to make InvariantCulture recognize a comma as a decimal separator?

From Dev

Show comma instead of point as decimal separator

From Dev

NumericUpDown: accept both comma and dot as decimal separator

From Dev

Read txt file with comma decimal separator in MATLAB

From Dev

NumericUpDown: accept both comma and dot as decimal separator

From Dev

Show comma instead of point as decimal separator

From Dev

Read txt file with comma decimal separator in MATLAB

From Dev

bc: decimal separator comma vs. point

From Dev

jQuery Validation Plugin: validate decimal number with comma as decimal separator

From Dev

Treat Comma and Period as Decimal Separator in Navision Decimal Fields

From Dev

formatting decimal number to string with comma as decimal separator in racket

From Dev

JavaScript regex to accept numbers with comma separator (thousands) and dot separator (decimals)

From Dev

Best practice to sum and format numbers to dollar values with 2 decimal points?

From Dev

how to get a greater value with numbers containing 2 decimal points

From Dev

Jquery/Javascript Regular expression to filter decimal numbers with comma, 13 int places and 2 decimal places

Related Related

  1. 1

    Parsing numbers with a comma decimal separator in JavaScript

  2. 2

    Google form regex for numbers with comma as decimal separator

  3. 3

    Only add thousand separator before decimal comma

  4. 4

    Pasting decimal numbers in excel / comma and point decimal separator

  5. 5

    Convert a double to 2 decimal places with comma separator

  6. 6

    Convert a double to 2 decimal places with comma separator

  7. 7

    Masked EditText with comma as decimal separator

  8. 8

    seq uses comma as decimal separator

  9. 9

    Pandas convert numbers with a comma instead of the point for the decimal separator from objects to numbers

  10. 10

    Format a decimal with comma as decimal separator and without thousands separator

  11. 11

    Getting numbers behind decimal separator

  12. 12

    Add comma separator to axis labels

  13. 13

    Jaxb conversion of number with comma as a decimal separator

  14. 14

    Why django uses a comma as decimal separator

  15. 15

    How to make InvariantCulture recognize a comma as a decimal separator?

  16. 16

    Show comma instead of point as decimal separator

  17. 17

    NumericUpDown: accept both comma and dot as decimal separator

  18. 18

    Read txt file with comma decimal separator in MATLAB

  19. 19

    NumericUpDown: accept both comma and dot as decimal separator

  20. 20

    Show comma instead of point as decimal separator

  21. 21

    Read txt file with comma decimal separator in MATLAB

  22. 22

    bc: decimal separator comma vs. point

  23. 23

    jQuery Validation Plugin: validate decimal number with comma as decimal separator

  24. 24

    Treat Comma and Period as Decimal Separator in Navision Decimal Fields

  25. 25

    formatting decimal number to string with comma as decimal separator in racket

  26. 26

    JavaScript regex to accept numbers with comma separator (thousands) and dot separator (decimals)

  27. 27

    Best practice to sum and format numbers to dollar values with 2 decimal points?

  28. 28

    how to get a greater value with numbers containing 2 decimal points

  29. 29

    Jquery/Javascript Regular expression to filter decimal numbers with comma, 13 int places and 2 decimal places

HotTag

Archive