0

I have a regex \d+(?:[.,]\d+ that matches decimal strings with either dot or comma, like 123.123 or 123,123 so the value is valid.

Now my problem is to generate a Float value from the String which is either 123.123 or 123,123

2
  • Float#parseFloat(). Don't know if it'll work for the comma, though... Commented Jun 12, 2014 at 6:01
  • I suggest you not use Float or float if you can avoid it, try using double Commented Jun 12, 2014 at 7:01

1 Answer 1

1

You can use the below code.

String val = "123.123 ";
float f = Float.parseFloat(val);

String val = "123,123 "; // if other char(like comma) come, you can also write a regex to replace those set of char
val = val.replace(",",".");// assuming comma is used for decimals
float f = Float.parseFloat(val);
Sign up to request clarification or add additional context in comments.

2 Comments

Hmmm, wonder if the comma here is being used for a thousands separator or a decimal place...
I'm almost sure it's used for decimals.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.