Search code examples
javadoublebigdecimal

Double to BigDecimal Java


What would be the best way of changing all of these double values to BigDecimal? I guess I did not realize when I started I should use BigDecimal for financial based programs. I am quite new to Java and appreciate any and all feedback.

private static class TaxBracket {

    final double minSalary;
    final double maxSalary;
    final double taxRate;

    TaxBracket(double minSalary, double maxSalary, double taxRate) {
        this.minSalary = minSalary;
        this.maxSalary = maxSalary;
        this.taxRate = taxRate;
    }
}
private static TaxBracket[] singleFiler;
static {
    singleFiler = new TaxBracket[]
        {
            new TaxBracket(0.0, 9075.0, 0.10),      //Index 0 TierOne
            new TaxBracket(9076.0, 36900.0, 0.15),  //Index 1 TierTwo
            new TaxBracket(36901.0, 89350.0, 0.25), //Index 2 TierThree
            new TaxBracket(89351.0, 186350.0, 0.28),//Index 3 TierFour
            new TaxBracket(186351.0, 405100.0, 0.33),//Index 4 TierFive
            new TaxBracket(405101.0, 406750.0, 0.35),//Index 5 TierSix
            new TaxBracket(406751.0, Double.MAX_VALUE, 0.396)//Index 6 TierSeven
        };
}
private static TaxBracket[] jointFiler;
static {
    jointFiler = new TaxBracket[]
        {
            new TaxBracket(0.0, 18150.0, 0.10),      //Index 0 TierOne
            new TaxBracket(18151.0, 73800.0, 0.15),  //Index 1 TierTow
            new TaxBracket(73801.0, 148850.0, 0.25), //Index 2 TierThree
            new TaxBracket(148851.0, 226850.0, 0.28),//Index 3 TierFour
            new TaxBracket(226851.0, 405100.0, 0.33),//Index 4 TierFive
            new TaxBracket(405101.0, 457600.0, 0.35),//Index 5 TierSix
            new TaxBracket(457601.0, Double.MAX_VALUE, 0.396)//Index 6 TierSeven
        };
}
private static TaxBracket[] hohFiler;
static {
    hohFiler = new TaxBracket[]
        {
            new TaxBracket(0.0, 12950.0, 0.10),      //Index 0 TierOne
            new TaxBracket(12951.0, 49400.0, 0.15),  //Index 1 TierTow
            new TaxBracket(49401.0, 127550.0, 0.25), //Index 2 TierThree
            new TaxBracket(127551.0, 206600.0, 0.28),//Index 3 TierFour
            new TaxBracket(206601.0, 405100.0, 0.33),//Index 4 TierFive
            new TaxBracket(405101.0, 432200.0, 0.35),//Index 5 TierSix
            new TaxBracket(432201.0, Double.MAX_VALUE, 0.396)//Index 6 TierSeven
        };

Solution

  • You should:

    1. Change double to BigDecimal everywhere affected.
    2. Change the arguments of new TaxBracket(...) from e.g. 9075.0 to new BigDecimal("9075.0") throughout. Note the quotes. If you don't quote the values you won't solve anything.

    Alternatively you could change the constructor arguments to String and do new BigDecimal(...) inside the constructor, which might be neater.