Re: Convertion from String to Float



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!

On Jul 24, 11:52 am, Pedro Pinto <kubi...@xxxxxxxxx> 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


.



Relevant Pages

  • Re: cross platform use of set locale
    ... > and thats would mean i'd have to run everything through a formatter ... I think you're misusing locale. ... You have $1,000,000.01 in your account. ...
    (comp.lang.python)
  • Re: cross platform use of set locale
    ... >>hi there i need to set my locale so that when i output a number it's ... Should work cross platform. ... and thats would mean i'd have to run everything through a formatter ...
    (comp.lang.python)
  • Re: java.util.Formatter and Locales
    ... the Locale can only be set through the ... constructor, ... But then again, a Formatter can be ... but even querying the locale of this Formatter will be ...
    (comp.lang.java.programmer)
  • Re: cross platform use of set locale
    ... >>and thats would mean i'd have to run everything through a formatter ... >I think you're misusing locale. ... thats exactly what i'm trying to do, only having to do that for all my ...
    (comp.lang.python)