Convert string to Float not so trivial

Discussion in 'Java' started by Grzegorz Stasica, Nov 18, 2003.

  1. Hi,

    Of course this
    new Float("123.56").floatValue()
    code will convert to float but what about string like this "123,56".
    Comma is used by many countries in Europe to separate fraction.
    Please don't tell me that I've to use regular expressions to replace ,
    to . in order to receive float value.

    Rgs.
    Greg
     
    Grzegorz Stasica, Nov 18, 2003
    #1
    1. Advertising

  2. Grzegorz Stasica

    Jon Skeet Guest

    Grzegorz Stasica <> wrote:
    > Of course this
    > new Float("123.56").floatValue()
    > code will convert to float but what about string like this "123,56".
    > Comma is used by many countries in Europe to separate fraction.
    > Please don't tell me that I've to use regular expressions to replace ,
    > to . in order to receive float value.


    Use NumberFormat.parse instead, where you've created an instance of
    NumberFormat which uses the appropriate Locale.

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet, Nov 18, 2003
    #2
    1. Advertising

  3. Grzegorz Stasica wrote:
    > Of course this
    > new Float("123.56").floatValue()
    > code will convert to float


    And unnecessarily creates a throwaway object. Use Float.parseFloat() instead.

    > but what about string like this "123,56".
    > Comma is used by many countries in Europe to separate fraction.
    > Please don't tell me that I've to use regular expressions to replace ,
    > to . in order to receive float value.


    java.text.DecimalFormat.parse()
     
    Michael Borgwardt, Nov 18, 2003
    #3
  4. Grzegorz Stasica

    VisionSet Guest

    "Grzegorz Stasica" <> wrote in message
    news:bpcmtl$mpq$...
    > Hi,
    >
    > Of course this
    > new Float("123.56").floatValue()
    > code will convert to float but what about string like this "123,56".
    > Comma is used by many countries in Europe to separate fraction.
    > Please don't tell me that I've to use regular expressions to replace ,
    > to . in order to receive float value.
    >


    DecimalFormatSymbols sym = new DecimalFormatSymbols();
    sym.setDecimalSeparator(',');
    DecimalFormat form = new DecimalFormat("");
    form.setDecimalFormatSymbols(sym);
    Number num = form.parse("123,456");
    float f = num.floatValue();
    System.out.println(f);

    There is probably an appropriate locale option in there somewhere to.

    --
    Mike W
     
    VisionSet, Nov 18, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,319
    Roedy Green
    May 10, 2004
  2. bd
    Replies:
    0
    Views:
    660
  3. baibaichen

    trivial or non-trivial object

    baibaichen, Jan 12, 2006, in forum: C++
    Replies:
    3
    Views:
    941
    osmium
    Jan 12, 2006
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,628
    James Kanze
    Oct 8, 2009
  5. Daniel Firu
    Replies:
    2
    Views:
    277
    Jesús Gabriel y Galán
    Jan 17, 2009
Loading...

Share This Page