Format JavaScript numbers to trim zeroes and have commas

javascript format number with commas and decimal
javascript number format comma
tolocalestring
tolocalestring percent
tolocalestring not working in ie
javascript format number thousands separator jquery
jquery format number with commas and decimal
javascript parse number with thousands separator

Here's what I need:

121 => 121
14231.439400000 => 14,231.4394
1.123456789 => 1.12345679
-9012.4430001 => -9,012.4430001

I've tried the following: return input.toFixed(8).toLocaleString().replace(/0*$/, '').replace(/\.$/, '');

and that doesn't add commas. I've also looked at numeraljs, but that doesn't have the decimal features I require (specifying up to 8 decimals out, or trimming)

Any help?


This should work on any browser that supports locales and the options described on MDN:

return input.toLocaleString("en", { 
    useGrouping: true, 
    maximumFractionDigits: 8 
});

Demonstration

And here's an alternative solution adapted from Elias Zamaria's answer here:

var x = input.toFixed(8).split('.');
return x[0].replace(/\B(?=(\d{3})+(?!\d))/, ',') + 
       ('.' + x[1]).replace(/\.?0+$/, '');

Demonstration

Number formatting in JavaScript, Introduction into number formatting using string.replace in JavaScript. There are many different ways of printing an integer with a comma as a thousands separators in JavaScript. (dot) as a separator with numbers having digits after the decimal point Remove decimals from currency in AngularJS. Welcome to a short tutorial on how to format numbers in Javascript. So you might have just started with Javascript, and have to do all sorts of number formatting – Add leading zeroes, add trailing zeroes, remove zeroes, add commas, and round to a certain decimal point.


Or if you want to do it yourself:

function fixNumber(number){
    var roundedNumber = Math.round(parseFloat(number) * 100000000) / 100000000,
    	numberString = roundedNumber.toString(),
        parts = numberString.split('.'),
        integerPart = parts[0],
        decimalPart = parts.length > 1 ? parts[1] : '';

    integerPart = integerPart.split('').reverse().join('').replace(/(\d{3})/g, '$1,').split('').reverse().join('').replace(/^(-?),/, '$1');
    decimalPart = decimalPart.replace(/0+$/, '');
    
    return decimalPart.length ? integerPart + '.' + decimalPart : integerPart;
}

document.write(fixNumber(121) + '<br>');
document.write(fixNumber(14231.439400000) + '<br>');
document.write(fixNumber(1.123456789) + '<br>');
document.write(fixNumber(-9012.4430001) + '<br>');

How to Format Numbers In Javascript (Add Comma, Pad Zeroes), So you might have just started with Javascript, and have to do all sorts of number formatting – Add leading zeroes, add trailing zeroes, remove  This function was created because not every number is formatted in the same way. For example, the number 1234 might be formatted as 1,234 or even 1.234. The function takes the following arguments. nStr: This is the number to be formatted. This might be a number or a string. No validation is done on this input.


Internationalization Support is now readily available in all browsers and nodejs. The advantage to this is that it's fast and configurable. For example, you can specify a minimum number of decimal precision and a maximum.

// Decimal Style (1,234,56)
let formatter = new Intl.NumberFormat('en-US', { style: 'decimal', minimumFractionDigits: 0, maximumFractionDigits: 8 });
formatter.format(1234.5678); // result: "1,234.57"

// Currency Style ($1,234.56)
let formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 0, maximumFractionDigits: 2 });
formatter.format(28761232.291); // result: "$28,761,232"

Format Numbers With Commas In JavaScript, It basically takes any number and turns it into formatted string with the The number_format() function is available in PHP and I have been relying on PHP to​  As I understand the issue, you have a number with a value of 1000 or more. Carlo wants JS to automatically add a comma like this: 15,000. He does not want 15000. The issue I have is he also says that it has more than one decimal point. I believe if there is more than one decimal point it is a string,


Section 2.5. Formatting Numbers for Text Display, Even if the number is an integer, the resulting string has a decimal and two The function to remove commas also uses regular expressions, and looks like the about number formatting involves a comparatively new JavaScript method of the​  JavaScript doesn't have many built-in methods to format numbers. Most of the time customized code needs to be used. Refer below for a couple rounding methods that JavaScript offers, then next up is some custom code I wrote to do more advanced formatting. Details (Includes a function to format separators other than commas)


Remove Comma From String, Learn the many ways to use JS to remove one or more commas from a string. Lets say you have an input box where you display the number with a thousand  Number formatting in JavaScript There are many different ways of printing an integer with a comma as a thousands separators in JavaScript. One of the simplest ways is to use String.prototype.replace() function with the following arguments:


javascript format number with commas Code Example, Get code examples like "javascript format number with commas" instantly convert number to string with commas javascript · javascript format  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). I have tried the tostring