Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Convertion from String to Float

Thread view: 
Pedro Pinto - 24 Jul 2007 14:57 GMT
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
Lasse Reichstein Nielsen - 24 Jul 2007 15:05 GMT
> 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
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Daniel Pitts - 24 Jul 2007 15:05 GMT
> Hi there,
>
[quoted text clipped - 20 lines]
>
> 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
Pedro Pinto - 24 Jul 2007 15:52 GMT
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
CD1 - 24 Jul 2007 19:10 GMT
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!

> Thank you for the replies. The solution adopted was:
>
[quoted text clipped - 9 lines]
>
> Pedro Pinto
Roedy Green - 25 Jul 2007 08:08 GMT
>I'm having some dificulties in converting from string to float
For sample code for he various possible conversions, you can copy and
paste see http://mindprod.com/applet/converter.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.