Hi there,I'm having some dificulties in converting from string to float. I geta number format exception... This is suposed to be easy but i can'tseem 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 inputstring: "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!RegardsPedro Pinto
|
|
0
|
|
|
|
Reply
|
kubic62 (34)
|
7/24/2007 1:57:34 PM |
|
Pedro Pinto <kubic62@gmail.com> writes:> 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 nounnecessary 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-- 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.'
|
|
0
|
|
|
|
Reply
|
Lasse
|
7/24/2007 2:05:51 PM
|
|
On Jul 24, 6:57 am, Pedro Pinto <kubi...@gmail.com> wrote:> 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 PintoFirst, 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 intojava.text.NumberFormat
|
|
0
|
|
|
|
Reply
|
Daniel
|
7/24/2007 2:05:51 PM
|
|
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 againRegardsPedro Pinto
|
|
0
|
|
|
|
Reply
|
Pedro
|
7/24/2007 2:52:34 PM
|
|
Hi Pedro,As Daniel pointed out, you should use the NumberFormat/DecimalFormatclasses. Here's an example:NumberFormat formatter = NumberFormat.getInstance();float f = formatter.parse("1,685");The getInstance method creates an instance of NumberFormat in thecurrent locale (my locale is Brazilian Portuguese, which uses comma toseparate decimal values, and that's why it worked). You can pass aLocale 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!On Jul 24, 11:52 am, Pedro Pinto <kubi...@gmail.com> wrote:> 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
|
|
0
|
|
|
|
Reply
|
CD1
|
7/24/2007 6:10:37 PM
|
|
>I'm having some dificulties in converting from string to floatFor sample code for he various possible conversions, you can copy andpaste see http://mindprod.com/applet/converter.html-- Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
|
|
0
|
|
|
|
Reply
|
Roedy
|
7/25/2007 7:08:20 AM
|
|
|
5 Replies
79 Views
(page loaded in 0.054 seconds)
|