NumberFormat vs double and long

Discussion in 'Java' started by StephaneLeFou, Jun 11, 2008.

  1. Hi,

    I have a function which receives an amount (money) in fthe form of a
    String. The values received can be of various formats such as:

    "99,999.99"
    "99 999.99"
    "99 999 99"
    "99999999"

    and so on, depending on the user's input, because the input has not
    been formatted yet with the Struts Validator for example...

    However, the maximum valid number is: 99999.99d

    I'm trying to figure out a way of checking if the passed String (once
    parsed and so on) is no bigger than 99999.99d

    So far I've been unsuccessful with the parse() and format() with
    NumberFormat. Sometimes, it converts it to a double, sometimes to a
    long...

    Any help would be greatly appreciated.
    Thanks.
     
    StephaneLeFou, Jun 11, 2008
    #1
    1. Advertisements

  2. StephaneLeFou

    Stefan Ram Guest

    A specification of the syntax and semantics of the
    input language used for the numerals is required.
    »Various formats such as ...« is not such a specification.
     
    Stefan Ram, Jun 11, 2008
    #2
    1. Advertisements

  3. Oh, I meant: any unsigned number passed in a string that could fill a
    mask like 9(9). The MaxLenght of the string is 9 characters and may
    or may not include: commas, spaces and decimals.

    Thanks again.
     
    StephaneLeFou, Jun 11, 2008
    #3
  4. StephaneLeFou

    cherryx Guest

    Assuming your string is in a variable called a:

    //remove anything that isn't a number or a dot
    a = a.replaceAll("[^0-9\\.]", "");

    //Convert from String to a number variable
    BigDecimal b = new BigDecimal(a);

    if (b.compareTo(new BigDecimal("99999.99"))==1)
    {
    System.out.println("Number is bigger than 99999.99");

    }



    Regards
    Chinthaka Weerasinghe
    [YOUR FRIENDLY JAVA TUTOR]
     
    cherryx, Jun 11, 2008
    #4
  5. Works like a charm and couldn't be much more simpler than that.

    Thank you very much, teacher :)
     
    StephaneLeFou, Jun 12, 2008
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.