Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default numbers with two decimal places

    hey, wanna calculate some normal stuff, but my code is not giving out the exact decimal places, it is always putting a 0 there... i have no clue why...
    this is my code:


    public class rechnen
    {
    private double[] array = {1,2,3,4,5,6,7,8,9,10,11,30};

    public void Stats()
    {

    double q1 = array[((array.length)/4)];

    double q2 = array[((array.length+1)/2)];

    double q3 = array[((array.length*3)/4)];

    double lr = (double)(((1.5)*(q3-q1)));

    System.out.println(q1);
    System.out.println(q2);
    System.out.println(q3);
    System.out.println(lr);
    System.out.println(q1);
    }

    public static void main(String[] args)
    {
    rechnen app = new rechnen();
    app.Stats();
    }
    }


    and it is giving out:

    4.0
    7.0
    10.0
    9.0
    4.0


    but the 10.0 should be 10.5 so what is going wrong here and if i wann sort this number with radix sort, do i have to change all the int into double or sth. else..

    thank u so much that u r always helping me...

    little_polarbear

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    The contents of array[] are all defined by int values. When they are converted to double, they will all have .0 endings.

    Why should the numbers being printed out be different from what is in array?

    When numbers in an expression are int before they are converted to double, the values will have no decimal places.
    If you want an expression to return double, you'll have to change one or more of the operands to a double. For example:
    change:array.length/4
    to: array.length/4.0

    But since these values are used as indexes to array, this isn't what you want to do.

    BTW array is a POOR name for a variable. It makes it hard to talk about an array named array without confusion. Name it to describe what it contains.
    Last edited by Norm; 08-26-2008 at 12:07 AM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    And once you put floating values in your array and get that working, I'll ask if you are commiting the usual sin of using float/double to store money.

    Don't do that!
    Its a sin.

  5. #5
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    ok, i don't wanna store money, i just need to do some stupid calculations... apparently it is not really working... i changed the programm so it is looking like this:

    public class rechnen
    {
    private float[] array = {(float) 1.00, (float)2.00, (float)3.00, (float)4.00, (float)5.00, (float)6.00, (float) 7.00, (float) 8.00, (float)9.00, (float)10.00,(float) 11.00, (float)30.00};

    public void Stats()

    {

    double plus = (float) (array[1] / 2.00);

    double q1 = (float)(array[(int)((array.length)*0.25)]);

    double q2 = (float)(array[(int) ((array.length+1)*0.50)]);

    double q3 = (float)(array[(int) (array.length*0.75)]);

    double lr = (float)(q1- ((1.50)*(q3-q1))) ;

    double ur = (float)(q3+((1.50)*(q3-q1))) ;

    System.out.println(q1);
    System.out.println(q2);
    System.out.println(q3);
    System.out.println(lr);
    System.out.println(ur);
    System.out.println(plus);


    for(int i = 0 ; i < array.length ; i++)

    {

    if(array[i] < lr)
    {
    System.out.println(array[i] + " ist ein outlier");
    }
    if(array[i] > ur)
    {
    System.out.println(array[i] + " ist ein outlier");
    }

    else
    {
    i++;
    }
    }
    }

    public static void main(String[] args)
    {
    rechnen app = new rechnen();
    app.Stats();
    }
    }


    but i mean i don't wanna put float in front of all my array numbers because i wanna read then in from an excel file and the main problem here is, that 1 / 2 is 0.5 and it is giving out 1, so can anyone help me please...

    little_polarbear

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    If you want an expression to return double, you'll have to change one or more of the operands to a double. For example:
    change:array.length/4
    to: array.length/4.0
    1 / 2 is 0.5 and it is giving out 1
    Does it give a 1 or 0?

    Why do you declare array to be float and then assign its values to doubles? double plus = (float) (array
    If array is type float, you don't need to cast it to float: (float)(array[...

    You need to learn how java converts numbers from one primitive type to another. Write a short, simple program to demo what happens when you do arithmetic with a mixture of int and double.
    Read your textbook to see what the defaults for numeric constants and how to enter them: 1 vs 1.0 vs 1.0F or 1D and 1L

  7. #7
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    to be honest i didn't know exactly what i was doing, i was just reading sth about it and it said do this and that...

    it is really giving out 0. which is really strange

    little_polarbear

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    it is really giving out 0
    Which out the code, 'it' is hard to understand.

  9. #9
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    i was relating to ur message before, but i managed it... it don't know what went wront but i finally worked it out...

    thank u so all so much for ur help

    little_polarbear

Similar Threads

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 02:01 AM
  2. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 12:04 AM
  3. Why my program cannot calculate the decimal value?
    By pearllymary78 in forum New To Java
    Replies: 4
    Last Post: 06-23-2008, 12:52 AM
  4. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  5. Capping decimal places
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-20-2007, 09:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •