Skip to content
Advertisement

Format number with thousands separator in Excel using Apache POI

I want to format some number cells, with a comma as thousands separator. For example:

JavaScript

I have the following code. What should I use as formatStr? Is there an easy way? Or do I have to detect the number of zeros in order to produce something like this #,###,###?

JavaScript

Keep in mind that I’m dealing with numbers. The cell type will be numeric, not string.

Update

enter image description here

Advertisement

Answer

Just #,### or #,##0 should be sufficient. Excel interprets this as having thousands separators every three digits (not just before the last three, which I infer is what you were expecting).

enter image description here

In the spirit of teaching a man to fish, this is how you can find out for yourself:

Format as Number, 0 decimal places, with 1000 separator:

enter image description here

Click OK, then re-open the number format dialog and go to Custom. Have a look at the formatting code (“Type”). It says #,##0, which for me gives the exact same result as #,###.

enter image description here

Advertisement