# Thread: numbers with two decimal places

1. Member
Join Date
Jun 2008
Posts
54
Rep Power
0

## 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. 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. 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.

4. Member
Join Date
Jun 2008
Posts
54
Rep Power
0
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

5. 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

6. Member
Join Date
Jun 2008
Posts
54
Rep Power
0
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

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

8. Member
Join Date
Jun 2008
Posts
54
Rep Power
0
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

#### Posting Permissions

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