user1908747
user1908747

Reputation: 11

Multiplying with a string in java

I have a string and want to multiply that string by .174.

I have String strNumber = numberText.getText();. How to I proceed from here?

Upvotes: 0

Views: 5993

Answers (3)

Mawia
Mawia

Reputation: 4310

This might help.

String strNumber = numberText.getText();;
    if (strNumber.contains(".")) {
        System.out.println(Double.parseDouble(strNumber) * 174);
    } else {
        System.out.println(Integer.parseInt(strNumber) * 174);
    }

Upvotes: 1

Alex Coleman
Alex Coleman

Reputation: 7326

Convert the string to whatever number datatype is appropriate (long if a big number, float/double for decimals etc.) using Long.parseLong(String) or Integer.parseInteger(String) etc. Then you can simply multiply the two numbers. Example:

String string1 = ".5";//Double in a string
String string2 = "6"; //Integer in a string
double multiplied = Double.parseDouble(string1) * Integer.parseInt(string2) * 3; //.5 * 6 * 3 = 9.0; number form (not string)

Upvotes: 6

Makoto
Makoto

Reputation: 106430

Before you can do anything with the number, you have to convert the value to an floating-point value. You can use the following approach:

double num = Double.parseDouble(numberText.getText())

Then you can perform your multiplication.

Upvotes: 3

Related Questions