Search code examples
javadoublebigdecimal

Change Doubles to BigDecimals


I Have this:

double priceMulti = 1.3;
double price = Double.parseDouble(jTextField1.getText());

//some if's and else's        

double date = (1980 * 2);
double random = Math.random()*15;
jLabel28.setText(String.valueOf((priceMulti * price * date * random)/price));
double copies = Double.parseDouble(jLabel28.getText());
jLabel33.setText(String.valueOf(copies / price));

code, and I want to change the variables of price and copies (whose are doubles) to BigDecimals. I Know that there are some topics like those, but my code is a bit different. Solved, thanks!


Solution

  • Change Doubles to BigDecimals

    Look at the constructor BigDecimal(String)

        double val = 5.2;
        BigDecimal bd = new BigDecimal(String.valueOf(val));
        System.out.println(bd);
    

    Explore other constructors also of BigDecimal.