How to change the grouping separator (thousands) of DecimalFormat from comma/point to quote using patterns?

CMorales

I have this piece of code where I try to replace the thousands separator (.) for quote (') with this pattern: ###'###,##

import java.text.*;

  public class NumberFormatTests
  {
    public static void main (String[] args)
    {
      double amount = 100100.543;
      NumberFormat nf = new DecimalFormat("###'###.##");
      System.out.println( "amount with    formatting: " + nf.format(amount) );
    }
  }

but I get this error:

Exception in thread "main" java.lang.IllegalArgumentException: Malformed pattern "###'###.##" at java.text.DecimalFormat.applyPattern(DecimalFormat.java:3411) at java.text.DecimalFormat.(DecimalFormat.java:436) at NumberFormatTests.main(NumberFormatTests.java:8)

I know it can be changed programatically by using DecimalFormatSymbols like this:

import java.text.*;
import java.util.Locale;

  public class NumberFormatTests
  {
    public static void main (String[] args)
    {
      double amount = 100100.543;

      Locale currentLocale = new Locale("de", "DE");
      DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);
      unusualSymbols.setGroupingSeparator('\'');

      NumberFormat nf = new DecimalFormat("###,###.##", unusualSymbols);
      System.out.println( "amount with    formatting: " + nf.format(amount) );
    }
  }

but I need to find a way to do it within the pattern, something like "###'###.##". I already tried "###''###.##" but the quote is added as suffix.

Is there any way to replace the thousands separator already within the pattern and not programatically?

Donat

Can you use a subclass of DecimalFormat instead? You could analyze the pattern in the subclass and extract the unusual grouping separator from the pattern. On a call of the subclass's format method, you can use this grouping separator with the original DecimalFormat as shown in your own example code. This is the result that is returned by the format method of the subclass.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change the decimal separator of DecimalFormat from comma to dot/point?

no grouping separator char for DecimalFormat

How to change thousands separator to apostrophe in format string

how to change thousands separator for blank in R plotly?

How to determine Thousands Separator using Format in VBA

Replace grouping separator of DecimalFormat in formatted value

How can I change the (locale) thousands separator in Python to Arabic Unicode separator?

How to force dot on thousands separator?

How to set thousands separator in Java?

How to the ISU thousands separator (space) ?

How to set thousands separator in R?

Change decimal and thousands separator in axis and tooltip

How to set space separator to float DecimalFormat?

How to get the Currency formatter with decimal and grouping separator in swift using ISOCode

Is it possible to format number with thousands separator using fmt?

How to change separator in a new file using awk

How to show thousands separator for pandas value counts?

How to format numbers with no grouping separator

How to change separator in a list from "," to ";" not using the join function or any list operations

How to use awk separator of single quote?

SSRS How to change grouping and columns by using parameter

How to change the separator on the clock?

Format number with thousands separator in Excel using Apache POI

python thousands separator formatting with different character without using locale

Read in numbers from csv file with point as thousands separator

UiPath - parse numeric string with thousands separator from excel to number

Remove thousands separator from a line starting with Total Value

How to get Excel to use digit grouping separator (thousand separator) universally?

How to change #new-quote:before background using JQuery or JavaScript?