Search code examples
javainputpromptbigdecimal

Java input Prompt for BigDecimal


I need users to be able to input numbers in the BigDecimal data type.

Numbers[i] = (BigDecimal)JOptionPane.showInputDialog(frame,"Type in a Number",0);

This is the code I tried to use but I get an error saying "Cannot cast from String to BigDecimal."

Any Advice?


Solution

  • You should read docs first. https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

    BigDecimal has a constructor which takes a string as a representation of your decimal.

    Number num = new BigDecimal(JOptionPane.showInputDialog(frame,"Type in a Number",0));