Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LargeValueFormatter looks to be broken for locale English(Australia) #4237

Closed
amitks2015 opened this issue Sep 21, 2018 · 1 comment
Closed

Comments

@amitks2015
Copy link

amitks2015 commented Sep 21, 2018

I am using LargeValueFormatter to format Y-axis values. It works fine for all the locales except for English(Australia).
e.g- it returns values like 4K, 8K, 16K etc, for all other locales but when I change the locale to English(Australia) it returns the same values as 4e03, 8e03, 16e3 ....

How can I fix this? My doubt is on the DecimalFormat instance as it uses locale internally.

  • Device: [Emulator, Samsung S7, Moto Z]
  • Android Version [8, 9]
  • Library Version (v 3.0.3)
@amitks2015 amitks2015 changed the title LargeValueFormatter looks to be broken for locale en_AU LargeValueFormatter looks to be broken for locale English(Australia) Sep 21, 2018
@amitks2015
Copy link
Author

amitks2015 commented Oct 23, 2018

Yes because of locale issue. "E" becomes lowercase then regex is no longer work.

While waiting for official patch from MPAndroidChart, you can temporary fix by creating your own LargeValueFormatter, similar to original source code, but has small modification in constructor:

public LargeValueFormatter() {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
    mFormat  = (DecimalFormat)nf;
    mFormat.applyPattern("###E00");
}

or change in makePretty():

private String makePretty(double number) {

    String r = mFormat.format(number);

    int numericValue1 = Character.getNumericValue(r.charAt(r.length() - 1));
    int numericValue2 = Character.getNumericValue(r.charAt(r.length() - 2));
    int combined = Integer.valueOf(numericValue2 + "" + numericValue1);

    r = r.replaceAll("E|e[0-9][0-9]", SUFFIX[combined / 3]);

    while (r.length() > MAX_LENGTH || r.matches("[0-9]+\\.[a-z]")) {
        r = r.substring(0, r.length() - 2) + r.substring(r.length() - 1);
    }

    return r;
}

source: https://stackoverflow.com/questions/52437345/largevalueformatter-looks-to-be-broken-for-locale-englishaustralia/52552054#52552054

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant