Results 1 to 9 of 9
Thread: numbers with two decimal places
 08252008, 11:50 PM #1Member
 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)*(q3q1)));
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
 08262008, 12:02 AM #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; 08262008 at 12:07 AM.
 08262008, 03:21 AM #3
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
Array index always take as an int value. Think about that.
 08262008, 04:52 AM #4
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.
 08262008, 07:44 PM #5Member
 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)*(q3q1))) ;
double ur = (float)(q3+((1.50)*(q3q1))) ;
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
 08262008, 08:09 PM #6If 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.01 / 2 is 0.5 and it is giving out 1
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
 08272008, 08:19 PM #7Member
 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
 08272008, 08:25 PM #8it is really giving out 0
 08272008, 11:04 PM #9Member
 Join Date
 Jun 2008
 Posts
 54
 Rep Power
 0
Similar Threads

round to two decimal places
By javaMike in forum New To JavaReplies: 3Last Post: 12242011, 03:01 AM 
rounding double to two decimal places
By javaMike in forum Advanced JavaReplies: 15Last Post: 03102010, 01:04 AM 
Why my program cannot calculate the decimal value?
By pearllymary78 in forum New To JavaReplies: 4Last Post: 06232008, 12:52 AM 
Java calculator decimal
By cart1443 in forum New To JavaReplies: 2Last Post: 04162008, 01:19 PM 
Capping decimal places
By Rageagainst20 in forum New To JavaReplies: 1Last Post: 12202007, 10:28 PM
Bookmarks