## regex format string number with commas and 2 decimals in javascript

jquery format number with commas and decimal

javascript parse number with thousands separator

regular expression for comma separated numbers in javascript

javascript format number thousands separator jquery

javascript number to string format

javascript display integer with commas

javascript add commas to number while typing

I have a string `n`

which is a number.
The following adds the commas where needed, but I also want to change the number of decimals. It should also round where appropriate.

var parts = n.split("."); var num = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : "");

This turns a number like `1234.567`

in to `1,234.567`

.
But I need my number to look like: `1,234.57`

I tried taking `parts[1]`

and converting to a Number, then rounding, then concatenating it back; but that is error prone.

How can I get this result by altering my regex? Thanks.

**Edit: Sept 18, 2019**

Since new APIs are available, thought it would be helpful to point out that you can do this much more simply using `toLocaleString`

options:

const numbers = [1, 1000, 2345.67, 21589.334719999995]; const options = { minimumFractionDigits: 2, maximumFractionDigits: 2 }; numbers.forEach(num => { const formatted = Number(num).toLocaleString('en', options); console.log(formatted); });

**Number formatting in JavaScript,** Introduction into number formatting using string.replace in There are many different ways of printing an integer with a comma as a thousands separators in JavaScript. Regular expression matches a single digit \d followed by a three-digit sets only (? toFixed(2) // always two decimal digits .replace('. varrgx =/(\d+)(\d{3})/; while (rgx.test(x1)){. x1 =x1.replace(rgx,'$1'+','+'$2'); returnx1 +x2; This causes a comma to be added to every third numeric character. So for the number 123456 the string 123,456 is returned. Here are some more examples. addCommas(1000);// 1,000.

var val = Math.round(Number(n) *100) / 100; var parts = val.toString().split("."); var num = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : "");

**javascript format number with commas Code Example,** Get code examples like "javascript format number with commas" instantly right from function formatNumberWithCommas(x) {. 2. return x.toString().replace(/B(? toString().replace(/\B(?; number decimal separator fromat javascript Write regular expression to describe a languages consist of strings made Regular Expression to RegEx for decimal numbers with 'commas' or 'dots'

You could just use **toFixed**

var parts = (+n).toFixed(2).split("."); var num = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (+parts[1] ? "." + parts[1] : "");

That would also make `1234.0000`

to `1234.00`

, but you can avoid that by converting to a number in the ternary, as zero is falsy

**Print a number with commas as thousands separators in JavaScript,** Print a number with commas as thousands separators in JavaScript - Simple The "\B" keeps the regex from putting a comma at the beginning of the string. adds commas in undesirable places if there are more than 3 digits after the decimal point. var nf = new Intl.NumberFormat(); nf.format(number); // "1,234,567,890" For currency formatting, I used Number.prototype.toFixed() to convert a number to string to have always two decimal digits.. If you need to format currency for a different country / locale, you would need to add some modifications to the currencyFormat method.

i think this is useful

var n=1234.567 var parts = n.toFixed(2).split("."); var num = parts[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,") + (parts[1] ? "." + parts[1] : ""); console.log(num);

**RegEx for decimal numbers with 'commas' or 'dots' · GitHub,** RegEx for decimal numbers with 'commas' or 'dots'. additional-methods.js. /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:(\.|,)\d+)?$/. Comment on gist. Sign in to comment or Definition and Usage. The toFixed() method converts a number into a string, rounding to a specified number of decimals. Note: if the desired number of decimals are higher than the actual number, zeros are added to create the desired decimal length.

When it comes to formatting currency in JavaScript, toLocaleString is great. You don't need to combine it with `toFixed`

and it will not work in some cases (whole numbers, numbers with single-digit fractions). Here's an example function:

const formatCurrency = (num, locale = 'en-US', currency = 'USD', minimumFractionDigits = 2) => { if (isNaN(num)) { return num; } return num.toLocaleString(locale, {style: 'currency', currency, minimumFractionDigits}); };

No super-customized string format patterns and no chained methods that work in specific scenarios.

Here is an example fiddle

**Formatting Numbers for Text Display,** You want to display the results of numeric calculations with a fixed number of digits to the right of the decimal. 2.5.2 Solution. Two recent additions to the JavaScript Hi, I have some data that uses decimals and commas. For example 191,08 5.138,55 8.666,01 19,33 17.154,10 The data uses a decimal to split the thousands. And a comma to signify the decimal place. I found a formula to convert the comma to decimals but end up with: 17.154.10 which does not work

**Advanced Search,** Numbers may use a comma for the decimal place, but cannot use on a real / decimal / floating point / numeric string with no more than 2 digits past the decimal. Expression to validate currency formatted numbers without the I found 5 type of javascript number format functions which do different formatting and some of them have more than one solutions, however, if you've a cool number format function, do share it with us. Round Decimal Points. These two Javascript functions round numbers to the nearest hundredth, with two digits following the decimal point.

**Format Numbers With Commas In JavaScript,** It basically takes any number and turns it into formatted string with the thousands I can now include this function into my JavaScript library and do this on the client side. x1 = x1.replace(rgx, '$1' + ',' + '$2'); The same regular expression is used on the string before the decimal point as in the previous I am struggling to format numbers with decimals and commas. My source data, which is an excel file, has all numerical values rounded to 2 with a round formula. When I view the data from the input tool I see two decimal points (10.98). However, after being run through the join tool the formatting changes to (10.980000).

**number_format - Manual,** number_format ( float $number , int $decimals = 0 , string $dec_point = ". is given, number will be formatted without decimals, but with a comma (",") between same output as number_format() but also works with numbers bigger than 2^53. While I love it when a nifty REGEX expression can solve the problem the easiest solution in this scenario is to use the ToString() function in the Formula tool: If you have a look at the Formula tool help file you'll see an example using the 3rd (optional) parameter to tell Alteryx you want comas.

##### Comments

- I collapsed the whole thing to
`Number(parseFloat(n).toFixed(2)).toLocaleString('en');`

- @RobM. But I tested with 123949.1, it does not return me with 2 decimal point. Is there any workaround for it?
- I tested with n=1. Returns 1, expected 1.00 (does not work :'( )
- @daCoda, updated the answer with a fix for this.
- @daCoda Right, you would need to use the
`maximumFractionDigits`

option for that - n is a string, so I cant use toFixed(). n needs to stay a string for other reasons.
- @brno792 - I'm sorry to break it to you, but you can't round a string, no matter how hard you try.
- You could of course just slice of the end of the string with
`parts[1].slice(0,2)`

, but that would give you`1234.56`

as it doesn't round. - Yes, I am well aware of that.. your solution is no help though.
- Thankyou, thats what I am looking for
- Thanks for your answer. Typically, it's helpful to write a few remarks outside of the code block that explains how the code works for future readers.