How to format a Float Value with the device currency format?

后端 未结 4 2033
执念已碎
执念已碎 2021-01-31 07:30

I have an app that prints a calculated money value and I want to display that value with the default currency format.

For example in Europe you would write:

1.

4条回答
  •  感动是毒
    2021-01-31 08:02

    to get device locale you could use this:

    @TargetApi(Build.VERSION_CODES.N)
        public static Locale getCurrentLocale(Context c) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                return c.getResources().getConfiguration().getLocales().get(0);
            } else {
                //noinspection deprecation
                return c.getResources().getConfiguration().locale;
            }
    

    and then to do format:

            double num = 1323.526;
    
            NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(getCurrentLocale(getContext()));
            defaultFormat.format(num)
    

提交回复
热议问题