number conversion

Discussion in 'Java' started by mamta81, Aug 29, 2011.

  1. mamta81

    mamta81 Guest

    Hi all,

    I have written a number check function. The purpose is to know number
    of digits before and after decimal.


    public void checkNumber(double number){

    System.out.println("number ------------------->" + number );
    Double d = new Double(number);
    String n = d.toString();
    System.out.println("n" + n);

    for(int i =0; i< n.length();i++){
    if(n.charAt(i)=='.'){
    System.out.println("Is a decimal number");
    }
    }

    int index = n.indexOf(".");
    System.out.println("index" + index);
    String dec = n.substring(index + 1);
    System.out.println("dec " + dec);

    if(dec.length() > 6 ){
    System.out.println("only 6 digits allowed after decimal");
    }

    String num = n.substring(0,n.indexOf("."));
    System.out.println("num " + num);
    if(num.length()>10){
    System.out.println("Only 10 places allowed before decimal");
    }
    }
    }


    when i give checkNumber( 33333335.2534566d); as input i get the
    following o/p

    number ------------------->3.33333352534566E7
    n3.33333352534566E7
    Is a decimal number
    index1
    dec 33333352534566E7
    only 6 digits allowed after decimal
    num 3


    1) what happens to my input for which I get a wrong index of ". "?
    2) Is there any other way to find the number of digits before and
    after decimal?
     
    mamta81, Aug 29, 2011
    #1
    1. Advertising

  2. On 29.08.2011 20:01, mamta81 wrote:

    > when i give checkNumber( 33333335.2534566d); as input i get the
    > following o/p
    >
    > number ------------------->3.33333352534566E7
    > n3.33333352534566E7
    > Is a decimal number
    > index1
    > dec 33333352534566E7
    > only 6 digits allowed after decimal
    > num 3
    >
    >
    > 1) what happens to my input for which I get a wrong index of ". "?


    But you *did* get the correct index.

    > 2) Is there any other way to find the number of digits before and
    > after decimal?


    I don't know. You can try by first formatting the String the way you
    expect it to be shown, which also includes defining the maximum number
    of fraction digits.

    For example:

    public void checkNumber(double number) {

    // UK locale definitely has "." symbol for decimal point
    NumberFormat nf = NumberFormat.getInstance(Locale.UK);

    nf.setGroupingUsed(false); // don't group digits
    nf.setMaximumFractionDigits(MAX_FRACTION_DIGITS);

    String numberString = nf.format(number);
    String[] nsArray = numberString.split("\\.");

    System.out.println("Before decimal point: " + nsArray[0].length());
    System.out.println("After decimal point: " + nsArray[1].length());
    }

    This will work as expected most of the time. :) To see when and why it
    won't work as expected, read some literature about floating point numbers.

    And now please answer this. Why are you trying to do that? What's the
    underlying reason?
     
    Screamin Lord Byron, Aug 29, 2011
    #2
    1. Advertising

  3. mamta81

    Roedy Green Guest

    On Mon, 29 Aug 2011 11:01:44 -0700 (PDT), mamta81
    <> wrote, quoted or indirectly quoted someone who
    said :

    >1) what happens to my input for which I get a wrong index of ". "?


    Java starts numbering slots at 0 not 1. Your program is correct.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
    the search for a superior moral justification for selfishness.
    ~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
     
    Roedy Green, Aug 29, 2011
    #3
  4. mamta81

    Roedy Green Guest

    On Mon, 29 Aug 2011 11:01:44 -0700 (PDT), mamta81
    <> wrote, quoted or indirectly quoted someone who
    said :

    >2) Is there any other way to find the number of digits before and
    >after decimal?


    Your code could be simplified by using

    final int place = s.indexof( '.' );

    n is traditionally a count, not a String. This dates back to FORTRAN
    days.

    Since the number you are looking at is expressed in binary, it really
    does not have a number of decimal places. You can choose any number
    of places you please in the string representation if you use
    DecimalFormat. You are just measuring an artifact of Java's default
    double -> String conversion method.
    See http://mindprod.com/applet/converter.html

    For some completely different techniques see:
    http://mindprod.com/jgloss/widthindigits.html
    http://mindprod.com/jgloss/log.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
    the search for a superior moral justification for selfishness.
    ~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
     
    Roedy Green, Aug 29, 2011
    #4
  5. mamta81

    Lew Guest

    Roedy Green wrote:
    > mamta81 wrote, quoted or indirectly quoted someone who said :
    >> 1) what happens to my input for which I get a wrong index of ". "?

    >
    > Java starts numbering slots at 0 not 1. Your program is correct.


    Well, not really correct at all, but you are getting the correct index.

    Quoting the OP's code:

    >


    Incomplete example. This code will not compile.

    > public void checkNumber(double number){
    >
    > System.out.println("number ------------------->" + number );


    Space missing after label.

    > Double d = new Double(number);


    Why do you bother creating an instance of 'Double'?

    > String n = d.toString();


    Use of the default formatting and expecting different results.

    > System.out.println("n" + n);


    Space missing after label.

    > for(int i =0; i< n.length();i++){


    Explicit loop instead of standard API call. Recomputation of 'n.length()' in every loop pass.

    > if(n.charAt(i)=='.'){


    Expensive call to 'charAt()' in lieu of iteration through code points, or at least the char sequence.

    > System.out.println("Is a decimal number");


    Space missing before label.

    > }


    No indentation.

    > }
    >
    > int index = n.indexOf(".");


    Redundant use of the API call after having already found the value the hard way. Why calculate the value twice, much less two completely different ways?

    > System.out.println("index" + index);


    Space missing after label.

    > String dec = n.substring(index + 1);
    > System.out.println("dec " + dec);
    >
    > if(dec.length() > 6 ){


    Magic value '6'. Why that value? No comments. Who says 6 is the right number?

    > System.out.println("only 6 digits allowed after decimal");
    > }
    >
    > String num = n.substring(0,n.indexOf("."));


    Third recomputation of the same value.

    > System.out.println("num " + num);
    > if(num.length()>10){


    Magic value '10'. Who says 10 is the right number?

    > System.out.println("Only 10 places allowed before decimal");
    > }
    > }
    > }


    Constants should at minimum be pulled into (static) final variables.

    --
    Lew
     
    Lew, Aug 29, 2011
    #5
  6. mamta81

    Roedy Green Guest

    On Mon, 29 Aug 2011 14:17:31 -0700 (PDT), Lew <>
    wrote, quoted or indirectly quoted someone who said :

    >Well, not really correct at all, but you are getting the correct index.


    exactly. I did not have the patience to do what you did.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
    the search for a superior moral justification for selfishness.
    ~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
     
    Roedy Green, Aug 29, 2011
    #6
    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. Mark Helm

    String to number conversion

    Mark Helm, Mar 5, 2004, in forum: XML
    Replies:
    0
    Views:
    416
    Mark Helm
    Mar 5, 2004
  2. Eugene
    Replies:
    0
    Views:
    353
    Eugene
    Jul 30, 2003
  3. Delali Dzirasa
    Replies:
    4
    Views:
    1,033
    Delali Dzirasa
    Sep 15, 2003
  4. Replies:
    14
    Views:
    1,042
    Simon Biber
    Nov 16, 2006
  5. , India
    Replies:
    2
    Views:
    478
    Fraser Ross
    Sep 15, 2009
Loading...

Share This Page