Double to Integer conversion

Need your help, please.

For the precision, I made the key variables type of "double".

However, when I complete the calculation, I wanted to get the integer portion only by truncating below the decimal point.I tried to use (int) () command.

However, compiler complains that I will loose the precision and does not compile. I wanted to loose by truncation...... What am I doing wrong?

Can you help , please?

Thanks

Kim

[459 byte] By [qls_kima] at [2007-11-26 20:15:48]
# 1
> What am I doing wrong?Hard to say. Can you post some code?Kaj
kajbja at 2007-7-9 23:22:36 > top of Java-index,Java Essentials,New To Java...
# 2
complaining = warningdoes not compile = error???
DrLaszloJamfa at 2007-7-9 23:22:36 > top of Java-index,Java Essentials,New To Java...
# 3

For our discussion, I copy the code from fast convergence calculation below.This may be terrible way of doing it but the code runs OK as long as I do not try to convert the double variable to integer.

It gives error message saying 搃ntValue[] in java.lang.Number.intValue can not be applied to double?

My question is what is the proper way of getting positive integer from double variable? I just wanted to get rid of the sign and truncate below the decimal point.Thanks.

while (n < 50) {

top = java.lang.Math.pow (-1, n);

bottom = java.lang.Math.pow (3.0, n) * (2*n +1);

top = (top / bottom) * factor;

pi = pi + top;

value = pi * co;

// need to truncate below the decimal (or pick interger portion)

//current = java.lang.Number.intValue (pi);

//System.out.println ("current =" + current);

//if(current == last) {

System.out.print ("pi = " + value);

System.out.println ("top = " + top);

n = n+1;

if (top == 0) {

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

break;

}

}

qls_kima at 2007-7-9 23:22:36 > top of Java-index,Java Essentials,New To Java...
# 4
This is how you castdouble d = 3.14;int i = (int)d;That should work fine.Kaj
kajbja at 2007-7-9 23:22:37 > top of Java-index,Java Essentials,New To Java...
# 5
> if (top == 0)top is a double? You should be testing it to see if it is *close enough* to 0:if (Math.abs(top) < YOUR_EPS)
DrLaszloJamfa at 2007-7-9 23:22:37 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks for your guidance, and deeply appreciated.Your example works find and I just learn to adopt in my code.Thansk again for your help.Kim
qls_kima at 2007-7-9 23:22:37 > top of Java-index,Java Essentials,New To Java...