-4

How I can get a decimal number from string to use it for calculations and math ? using android studio ( Java )

String a = "0.1234"
String b = "0.4321"
total = a * b 

3 Answers3

0

you can try

Float.valueOf(String)

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

You can do as Arju said or alternative is to parseFloat

float floatA = Float.parseFloat(a);
float floatB = Float.parseFloat(b);
float total = floatA*floatB;
kjurkovic
  • 4,044
  • 2
  • 23
  • 28
0

This will do:

String a = "0.1234"
float aFloat = Float.parseFloat(a);

String b = "0.4321"
float bFloat = Float.parseFloat(b);

total = aFloat * bFloat

This might throw a NumberFormatException, so you will have to put it in a try block.

FD_
  • 12,947
  • 4
  • 35
  • 62