re: decimal place checking

Discussion in 'Java' started by timothy ma and constance lee, Nov 21, 2007.

  1. Dear Sir/Madam

    Sir

    I am trying to write a program which check the format:

    123.4567899 which is allowed.

    The max no. of digit for integer part is 3 and the max of digit after the
    decimal is 7 so total max length is 11.

    It is very strange that when enter 0.45 in the result, it is positive.
    But it is exception when enter 0.55 - 0.58

    public static void main(String[] args) {


    Double result = null;
    //can try from 0.55 - 0.58 wont work

    result = Double.valueOf((String)"0.55");
    double netResult = 0.00;
    double netResult1 = 0.00;
    int decimal = 7;
    netResult = result.doubleValue() * Math.pow(10,decimal+1);
    netResult1 = netResult % 1;
    if (netResult1 >0){
    System.out.print("Exception");
    }
    else{
    System.out.print("OK");
    }
    System.out.print("\nResult = "+result);
    System.out.print("\nResult.doubleValue =
    "+result.doubleValue());
    System.out.print("\nnet Result = "+netResult);
    System.out.print("\nnet Result1 = "+netResult1);
    }

    It is all OK for value except from 0.55 to 0.58 which throw exception,
    Any clue?

    Please kindly help
     
    timothy ma and constance lee, Nov 21, 2007
    #1
    1. Advertising

  2. timothy ma and constance lee

    Arne Vajhøj Guest

    timothy ma and constance lee wrote:
    > I am trying to write a program which check the format:
    >
    > 123.4567899 which is allowed.
    >
    > The max no. of digit for integer part is 3 and the max of digit after the
    > decimal is 7 so total max length is 11.


    Maybe regex and Java String "\\d{1,3}\\.\\d{1,7}" ?

    Arne
     
    Arne Vajhøj, Nov 21, 2007
    #2
    1. Advertising

  3. timothy ma and constance lee wrote:
    > Dear Sir/Madam
    >
    > Sir
    >
    > I am trying to write a program which check the format:
    >
    > 123.4567899 which is allowed.
    >
    > The max no. of digit for integer part is 3 and the max of digit after the
    > decimal is 7 so total max length is 11.
    >
    > It is very strange that when enter 0.45 in the result, it is positive.
    > But it is exception when enter 0.55 - 0.58


    I suspect there will be other cases that fail. The problem is that many
    numbers, such as 0.55, that meet your format requirements cannot be
    exactly represented as double. The result of the Double.valueOf call is
    the closest number that is representable as double, not the exact input.

    I would do this either as a String test, or look at BigDecimal.

    Patricia
     
    Patricia Shanahan, Nov 21, 2007
    #3
  4. Arne

    Can you show me the exact code?

    thanks

    "Arne Vajhøj" <> wrote in message
    news:4743a7ea$0$90268$...
    > timothy ma and constance lee wrote:
    >> I am trying to write a program which check the format:
    >>
    >> 123.4567899 which is allowed.
    >>
    >> The max no. of digit for integer part is 3 and the max of digit after the
    >> decimal is 7 so total max length is 11.

    >
    > Maybe regex and Java String "\\d{1,3}\\.\\d{1,7}" ?
    >
    > Arne
     
    timothy ma and constance lee, Nov 21, 2007
    #4
  5. timothy ma and constance lee

    Arne Vajhøj Guest

    timothy ma and constance lee wrote:
    > Can you show me the exact code?


    private static Pattern p = Pattern.compile("\\d{1,3}\\.\\d{1,7}");
    public static boolean is37digit(String s)
    {
    return p.matcher(s).matches();
    }

    You did not specify minimum of digits, so I guessed at 1.

    Arne
     
    Arne Vajhøj, Nov 21, 2007
    #5
    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. martin
    Replies:
    1
    Views:
    493
    Rick Strahl [MVP]
    Jul 19, 2004
  2. BoomerangThree
    Replies:
    3
    Views:
    429
    Jacob
    Aug 20, 2004
  3. shannon
    Replies:
    1
    Views:
    8,648
  4. shannon
    Replies:
    0
    Views:
    591
    shannon
    Feb 1, 2006
  5. timothy ma and constance lee

    re: decimal place checking using JDK1.3

    timothy ma and constance lee, Nov 21, 2007, in forum: Java
    Replies:
    10
    Views:
    518
    Patricia Shanahan
    Nov 21, 2007
Loading...

Share This Page