package com.platypii.baseline.util;

import android.support.annotation.Nullable;
import java.text.DecimalFormat;

public class Numbers {

    public static boolean isReal(double value) {
        return !Double.isNaN(value) && !Double.isInfinite(value);
    }

    /**
     * Fast integer power x^y
     */
    public static int pow(int x, int y) {
        // base cases
        if (x == 1 || y == 0) return 1;
        else if (y == 1) return x;
        else if (y == 2) return x * x;
        else if (y == 3) return x * x * x;
        // divide and conquer
        final int sqrt = pow(x, y / 2);
        if (y % 2 == 0) return sqrt * sqrt;
        else return x * sqrt * sqrt;
    }

    /**
     * Parse a string into a double, but use NaN instead of exceptions
     */
    public static double parseDouble(@Nullable String str) {
        if (str == null || str.isEmpty()) {
            return Double.NaN;
        } else {
            try {
                return Double.parseDouble(str);
            } catch (NumberFormatException e) {
                Exceptions.report(e);
                return Double.NaN;
            }
        }
    }

    public static float parseFloat(@Nullable String str) {
        if (str == null || str.isEmpty()) {
            return Float.NaN;
        } else {
            try {
                return Float.parseFloat(str);
            } catch (NumberFormatException e) {
                Exceptions.report(e);
                return Float.NaN;
            }
        }
    }

    public static int parseInt(@Nullable String str, int defaultValue) {
        if (str == null || str.isEmpty()) {
            return defaultValue;
        } else {
            try {
                return Integer.parseInt(str);
            } catch (NumberFormatException e) {
                Exceptions.report(e);
                return defaultValue;
            }
        }
    }

    public static final DecimalFormat format2 = new DecimalFormat("#.##");
    public static final DecimalFormat format3 = new DecimalFormat("#.###");
    public static final DecimalFormat format6 = new DecimalFormat("#.######");

}

@@ -105,16 +105,80 @@
 lFormat;
+%0Aimport java.text.DecimalFormatSymbols;%0Aimport java.util.Locale;
 %0A%0Apublic
@@ -1942,32 +1942,139 @@
        %7D%0A    %7D%0A%0A
+    private static final DecimalFormatSymbols formatSymbols = DecimalFormatSymbols.getInstance(Locale.US);%0A
     public stati
@@ -2129,16 +2129,31 @@
 t(%22#.##%22
+, formatSymbols
 );%0A    p
@@ -2220,16 +2220,31 @@
 (%22#.###%22
+, formatSymbols
 );%0A    p
@@ -2314,14 +2314,29 @@
 .######%22
+, formatSymbols
 );%0A%0A%7D%0A

Fix broken number formatting in 0,00 locales