0

I have the following code which works when I am multiplying whole numbers however when I try to multiply decimal numbers and display the answer as a decimal number I am getting an error

 public void actionPerformed(ActionEvent e) {

         int num1=Integer.parseInt(textArea_price.getText()); 
         int num2=Integer.parseInt(textArea_quantity.getText());

          int result = num1*num2;
          textField_Name.setText(Integer.toString(result));

Any suggestions on what I can change or add to make this code work with decimal numbers is appreciated.

7
  • 1
    Please state what error you're seeing, and on what line.
    – Hot Licks
    Mar 11, 2012 at 14:43
  • (And what do you mean by "whole numbers" vs "decimal numbers"?)
    – Hot Licks
    Mar 11, 2012 at 14:45
  • You are aware, that parseInt() can only parse int's, right?
    – quaylar
    Mar 11, 2012 at 14:45
  • The error message I am getting is 14:44.56 78[dbg] CShellExt::CShellException in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "12.34"
    – FatmaTurk
    Mar 11, 2012 at 14:45
  • @HotLicks He probably wants to parse floating point numbers..
    – quaylar
    Mar 11, 2012 at 14:46

6 Answers 6

4

BigDecimal could be what you want.

 textField_Name.setText(
       new BigDecimal(textArea_price.getText())
       .multiply(new BigDecimal(textArea_quantity.getText())).toString());
6
  • I'm trying to use what you have here but it's underlining "valueof" part without any recommended assumption. Do you know why that could be?
    – FatmaTurk
    Mar 11, 2012 at 15:02
  • @FatmaTurk: Note the spelling: valueOf()
    – quaylar
    Mar 11, 2012 at 15:35
  • @FatmaTurk If you use code completion after v it should suggest valueOf Mar 11, 2012 at 17:22
  • @PeterLawrey yeah it does give me the suggestion but then when I have the rest of the code (when its complete) I get this error message The method valueOf(long) in the type BigDecimal is not applicable for the arguments (String)
    – FatmaTurk
    Mar 11, 2012 at 22:22
  • Sorry, it has to be new BigDecimal(string) Mar 12, 2012 at 8:17
1

Change int/Integer to double/Double.

Note that double is imprecise (for example 1.2 * 9 gives 10.799999999 when it should be 10.8). Consider using BigDecimal if exactness is required.

1
  • 1
    My guess is that he would want BigDecimal, since he's dealing with money.
    – Hot Licks
    Mar 11, 2012 at 14:49
0

If you are expecting real numbers in the text fields then use double rather than int.

   double num1 = Double.parseDouble(textArea_price.getText());
0

Use Double.parseDouble() instead of Integer.parseInt(). And use double type of variables:

double num1 = Double.parseDouble(textArea.getText());

0

My guess is that you're using the term "decimal number" to (erroneously) refer to numbers containing a "decimal point". Such numbers are not integers and cannot be parsed by Integer.toString. They need to be handled as either floating point (float or double) or as BigDecimal.

0
float a = Float.parseFloat("121.12");
float b = Float.parseFloat("0.9");
float res = a*b;
String str = Float.toString(res);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.