Convertion from String to Float

P

Pedro Pinto

Hi there,

I'm having some dificulties in converting from string to float. I get
a number format exception... This is suposed to be easy but i can't
seem to figure it out, can someone give me some help?

Here's the code:

String cde = "1,685";
float num = Float.valueOf(cde).floatValue();


Exception:

Exception in thread "main" java.lang.NumberFormatException: For input
string: "1,685"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Float.valueOf(Unknown Source)
at Ascii.main(Ascii.java:8)

Thanks in advance for any help!

Regards

Pedro Pinto
 
L

Lasse Reichstein Nielsen

Pedro Pinto said:
String cde = "1,685";
float num = Float.valueOf(cde).floatValue();

You might want to look at Float.parseFloat which doesn't create
an intermediary Float object. However, that's not the problem.

The problem is the ','.

The expected format of floating point numbers uses American notation,
so the decimal point must be '.', not ','. There should be no
unnecessary characters, as, e.g., a thousands separator.

I.e., if the number you expect is 1 point something, write it as
"1.685". It it is thousand and something, write "1685".
Exception in thread "main" java.lang.NumberFormatException: For input
string: "1,685"

Tsk, tsk, bad error message. It could have said that it was an unexpected
','.

/L
 
D

Daniel Pitts

Hi there,

I'm having some dificulties in converting from string to float. I get
a number format exception... This is suposed to be easy but i can't
seem to figure it out, can someone give me some help?

Here's the code:

String cde = "1,685";
float num = Float.valueOf(cde).floatValue();

Exception:

Exception in thread "main" java.lang.NumberFormatException: For input
string: "1,685"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Float.valueOf(Unknown Source)
at Ascii.main(Ascii.java:8)

Thanks in advance for any help!

Regards

Pedro Pinto

First, you should use Float.parseFloat(), not valueOf().floatValue().
Second in Float.valueOf, "." is the decimal notation, not ",".

If you need to parse it in a locale specific way, look into
java.text.NumberFormat
 
P

Pedro Pinto

Thank you for the replies. The solution adopted was:

temp = value.split(",");
value = temp[0]+"."+temp[1];
numero = Float.parseFloat(value);


I'll use the replace later to avoid all these operations.

Thanks once again

Regards

Pedro Pinto
 
C

CD1

Hi Pedro,

As Daniel pointed out, you should use the NumberFormat/DecimalFormat
classes. Here's an example:

NumberFormat formatter = NumberFormat.getInstance();
float f = formatter.parse("1,685");

The getInstance method creates an instance of NumberFormat in the
current locale (my locale is Brazilian Portuguese, which uses comma to
separate decimal values, and that's why it worked). You can pass a
Locale object to specify a non-default locale.

If you need a more accurate formatting, try the DecimalFormat class.
You can construct an object of this class with a custom format.

See ya!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top