Number Value Format With Comma and Two Decimal Points

Number value format with comma and two decimal points

Javascript offers you few solutions to do that. First two coming to mind below.

1. number.toLocaleString

As already mentioned, .toLocaleString can help you, but instead of minimumFractionDigits use maximumFractionDigits.

Like below:

number.toLocaleString(undefined, { maximumFractionDigits: 2 })

So summarizing:

const decimalsFormated = number.toLocaleString(undefined, { maximumFractionDigits: 2 })

And than

const finalFormated = String(decimalsFormated).replace(/\B(?=(\d{3})+(?!\d))/g, ",");

2. Number.parseFloat + toFixed

let number = 123.1234
Number.parseFloat(number).toFixed(2);

In each approach, wrap your solution in function preferably:

function getCommaSeparatedTwoDecimalsNumber(number) {
const fixedNumber = Number.parseFloat(number).toFixed(2);
return String(fixedNumber).replace(/\B(?=(\d{3})+(?!\d))/g, ",");

}

You could also use regex. I would say it is overaly complicated though.

Also very important thing to notice is that you may or may not want to round your final outcome.

Using toLocaleString with maxDigits will just remove everything after those two digits.
Using toFixed will round your output unproperly.

This solution will round it properly:

Number(Math.round(1.005+'e2')+'e-2').toFixed(2);

Pasted from here:
Format number to always show 2 decimal places

Last thing, probably most important.
Depending on what format input number will have, above solution may or may not work. You need to decide on input format and if that cant be foreseen, provide formaters for each possibility:

1000000.123124

10000123123

100000,1239

1.12039

1,19012

etc.

And depending on format, order of actions you need to take may vary.

How to format comma separated value to two decimal places

This requires two operations...

  1. Parse the number string into a Number instance

    const actualNumber = +number.replace(/,/g, '') // produces 4500.02734
  2. Format the string to a given locale with a maximum of 2 decimal places

    const formatted = actualNumber.toLocaleString('en-US', {maximumFractionDigits: 2})

var number = '4,500.02734'
const actualNumber = +number.replace(/,/g, '')const formatted = actualNumber.toLocaleString('en-US', {maximumFractionDigits: 2})
document.write(formatted)

Format a number with commas and decimals in C# (asp.net MVC3)

int number = 1234567890;
Convert.ToDecimal(number).ToString("#,##0.00");

You will get the result 1,234,567,890.00.

Format a number with comma separators and round to 2 decimal places in Python 2?

Add a decimal point with number of digits .2f see the docs: https://docs.python.org/2/library/string.html#format-specification-mini-language :

In [212]:
"{0:,.2f}".format(2083525.34561)

Out[212]:
'2,083,525.35'

For python 3 you can use f-strings (thanks to @Alex F):

In [2]:
value = 2083525.34561
f"{value:,.2f}"


Out[2]:
'2,083,525.35'

Convert numbers into comma separated format with two decimals in javascript

You can use the minimumFractionDigits option of the toLocaleString function.

// result 3,000.00
Number(parseFloat(3000).toFixed(2)).toLocaleString('en', {
minimumFractionDigits: 2
});

// result 123,233.12
Number(parseFloat(123233.12).toFixed(2)).toLocaleString('en', {
minimumFractionDigits: 2
});

You can even remove the parseFloat,toFixed and Number function usage as well, if this is not used for some logic other than displaying the decimal value up to 2 digits.

Format double value to 2 decimal points and get comma separated currency format in Java

DecimalFormat and NumberFormat objects expect their objects to format to be some kind of number, not a string such as format1, so an IllegalArgumentException is thrown.

You've passed an already formatted string format1 to format instead of a number. Pass price to format as you already did the first time you called it.

But the US locale NumberFormat won't give you any digits beyond the decimal point; you will see 2,500 printed. If you want those 2 decimal digits, you can specify that format explicitly, similar to how you constructed df.

String format2 = new DecimalFormat("#,###.00").format(price);

regex format string number with commas and 2 decimals in javascript

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] : "");

Format BigDecimal number with commas upto 2 decimal places

You can use NumberFormat for what you want. Here is the function I use:

   public static String GenerateFormat(Double value) {
if (value == null) return "";
NumberFormat nf = NumberFormat.getInstance(new Locale("de", "DE"));
nf.setMinimumFractionDigits(0);
nf.setMaximumFractionDigits(2);
nf.setGroupingUsed(true);
return nf.format(value);
}

In this, you can see that I am providing Locale as a parameter for NumberFormat. Each country has its own number formatting standard, and what you want can be achieved with this locale new Locale("en", "US"). Inside setMaximumFractionDigits you can place how much of fraction digits you want, in your case it is 2.

EDIT

Because of your situation, try this:

   public static String GenerateFormat(Double value) {
if (value == null) return "";
NumberFormat nf = NumberFormat.getInstance(new Locale("de", "DE"));
if (value%1 == 0) nf.setMinimumFractionDigits(0);
else nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
nf.setGroupingUsed(true);
return nf.format(value);
}


Related Topics



Leave a reply



Submit