0

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
        };
Gabriel_W
  • 1,645
  • 5
  • 16
  • 26
  • Possible duplicate of [Convert double to BigDecimal and set BigDecimal Precision](http://stackoverflow.com/questions/12395281/convert-double-to-bigdecimal-and-set-bigdecimal-precision) – Nir Alfasi Jan 30 '17 at 23:08
  • 1
    Make them strings instead and pass them to the `BigDecimal` constructor. – shmosel Jan 30 '17 at 23:11
  • @alfasin Nothing there that looks like a duplicate to me. – user207421 Jan 30 '17 at 23:17
  • @alfasin Please continue the discussion about the duplicate here where it belongs. All I saw in the duplicate you suggested was about output formatting. – user207421 Jan 30 '17 at 23:38
  • @alfasin Read the question. It isn't about converting. It is about *changing*, in a context where `double` shouldn't be used *at all*. Nothing in the duplicate about that.. – user207421 Jan 30 '17 at 23:48
  • @alfasin That's exactly what it says in my answer posted 40 minutes ago. I don't see why I should now be asked to agree. I can't account for your feelings but no rebuke has been expressed. – user207421 Jan 30 '17 at 23:55

3 Answers3

1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • In reality all this should be in a database as DECIMAL columns, not in code. – user207421 Jan 30 '17 at 23:21
  • @alfasin There is no such method as `BigDecimal.valueOf(String)`, and `BigDecimal.valueOf(double)` is still invalid in terms of the question, which is, correctly, about not using `double` at all for financial calculations. You won't get the right answer for a tax rate of e.g. 0.33 if it's represented as a `double` *anywhere*, and I would have expected you to already know that. Please keep the discussion about the duplicate where it belongs, which isn't here. – user207421 Jan 30 '17 at 23:44
  • when you're using sentences like "read the question" and " I would have expected you to already know that" it makes the other side feel small. Maybe you didn't mean it - but that's how most people will feel when someone talks down at them like that. Being correct is important, but being able to communicate it in a way that won't hurt others feeling is not negligible as well. Wish you all the best! – Nir Alfasi Jan 30 '17 at 23:58
  • @alfasin Again I can't account for your feelings, only for the actual words I have used, which don't support them. 'Read the question' is standard fare here. 'I would have expected you to already know that' is actually a compliment, as I've seen a lot of your work here, and an expression of surprise. – user207421 Jan 31 '17 at 00:07
  • I shouldn't be surprised but it still amazes me how the written communication could be interpreted to the exact same opposite of the writer's meaning... My bad, thanks for your clarifications. – Nir Alfasi Jan 31 '17 at 00:19
0

You can do it like this (if you want to keep the double values as inputs) :

void changeToBigDecimal(){
    BigDecimal minSalaryDec = new BigDecimal(this.minSalary, MathContext.DECIMAL64);
    BigDecimal maxSalaryDec = new BigDecimal(this.maxSalary, MathContext.DECIMAL64);
    BigDecimal taxRateDec= new BigDecimal(this.taxRate, MathContext.DECIMAL64);
}

And call it after setting the initial values in your constructor. If you want to you can make them global variables instead of local ones too.

eetschel
  • 46
  • 1
  • 5
  • This does not 'change all the `double` values to `BigDecimal`' and is therefore not an answer to the question. – user207421 Jan 31 '17 at 02:36
-1

I recommend that you change the 3 double variables at the top of your TaxBracket class to BigDecimals. And in your constructor for TaxBracket, use the BigDecimal constructor that takes a double

It should look something like this when you're done:

private static class TaxBracket {

final BigDecimal minSalary;
final BigDecimal maxSalary;
final BigDecimal taxRate;

TaxBracket(double minSalary, double maxSalary, double taxRate) {
    this.minSalary = new BigDecimal(minSalary);
    this.maxSalary = new BigDecimal(maxSalary );
    this.taxRate = new BigDecimal(taxRate );
}
retodaredevil
  • 1,261
  • 1
  • 13
  • 20
  • This does not 'change all the `double` values to `BigDecimal`' and is therefore not an answer to the question. – user207421 Jan 31 '17 at 02:36