3

I am trying to apply this format to a given int number #´###,### I tried with DecimalFormat class but it only allows to have one grouping separator symbol when I need to have two the accute accent for millions and commas for thousands.

So at the end I can format values like 1,000 or millions in this way 1´000,000

  • check http://stackoverflow.com/questions/5114762/how-do-format-a-phone-number-as-a-string-in-java – Roshan Nov 09 '16 at 16:53
  • @Roshan the solutions in that question make use of `substring()` or `replace()`, which might be OK for phone "numbers", but that isn't the best idea when dealing with actual numbers (like e.g. money). OP would need to take into account negative numbers, numbers without a decimal point, etc. – walen Nov 09 '16 at 17:31
  • Thank you @Roshan! – Donald Fernandez Nov 09 '16 at 17:32

4 Answers4

1

I always prefer to use String.format, but I am not sure if there is a Locale that would format numbers like that either. Here is some code that will do the job though.

// Not sure if you wanted to start with a number or a string. Adjust accordingly
String stringValue = "1000000";
float floatValue = Float.valueOf(stringValue);

// Format the string to a known format
String formattedValue = String.format(Locale.US, "%,.2f", floatValue);

// Split the string on the separator
String[] parts = formattedValue.split(",");

// Put the parts back together with the special separators
String specialFormattedString = "";
int partsRemaining = parts.length;
for(int i=0;i<parts.length;i++)
{
    specialFormattedString += parts[i];
    partsRemaining--;
    if(partsRemaining > 1)
        specialFormattedString += "`";
    else if(partsRemaining == 1)
        specialFormattedString += ",";
}
1

Try this, these Locale formats in your required format.

    List<Locale> locales = Arrays.asList(new Locale("it", "CH"), new Locale("fr", "CH"), new Locale("de", "CH"));
    for (Locale locale : locales) {
        DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
        DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
        dfs.setCurrencySymbol("");
        df.setDecimalFormatSymbols(dfs);
        System.out.println(String.format("%5s %15s %15s", locale, format(df.format(1000)), format(df.format(1_000_000))));
    }

util method

private static String format(String str) {
    int index = str.lastIndexOf('\'');
    if (index > 0) {
        return new StringBuilder(str).replace(index, index + 1, ",").toString();
    }
    return str;
}

output

it_CH        1,000.00    1'000,000.00
fr_CH        1,000.00    1'000,000.00
de_CH        1,000.00    1'000,000.00

set df.setMaximumFractionDigits(0); to remove the fractions

output

it_CH           1,000       1'000,000
fr_CH           1,000       1'000,000
de_CH           1,000       1'000,000
Saravana
  • 12,647
  • 2
  • 39
  • 57
1

I found useful the link @Roshan provide in comments, this solution is using regex expression and replaceFirst method

public static String audienceFormat(int number) {
    String value = String.valueOf(number);

    if (value.length() > 6) {
            value = value.replaceFirst("(\\d{1,3})(\\d{3})(\\d{3})", "$1\u00B4$2,$3");
        } else if (value.length() >=5 && value.length() <= 6) {
            value = value.replaceFirst("(\\d{2,3})(\\d{3})", "$1,$2");
        }  else {
            value = value.replaceFirst("(\\d{1})(\\d+)", "$1,$2");
        }

    return value;
} 

I don't know if this solution has a performance impact, also I am rockie with regex, so this code might be shorted.

-1

Maybe try using this, the "#" in place with the units you want before the space or comma.

String num = "1000500000.574";
    String newnew = new   DecimalFormat("#,###.##").format(Double.parseDouble(number));