Umang Mathur
Umang Mathur

Reputation: 863

How to parse numerical values from a string?

How do I convert String in form xx:yy into integer form xxyy or float form xx.yy

For example If I have the following string.

String x = "10:30"

How do I get integer 1030 or float value 10.30 ?

Upvotes: 1

Views: 191

Answers (2)

Shobit
Shobit

Reputation: 794

String xInt = x.replace(":","");
Integer.parseInt(xInt);
String xFloat = x.replace(":",".");
Float.parseFloat(xFloat);

Upvotes: 1

regulus
regulus

Reputation: 1622

String[] parts = x.split(":");
float value = Integer.parseInt(parts[0]) + (float)Integer.parseInt(parts[1])/100;

or

Float.parseFloat(x.replaceAll(":", "."));

Upvotes: 2

Related Questions