Problem with converting Double into Integer.... help needed
hihi... I have used the following line of code in one of my Report generation (Jasper Report)
"String.valueOf(Math.Ceil((${XXX}.intValule()/${YYY}.intValue())*100))"
even the field to display the results I have preset it to java.lang.Integer...
my expected result would be a percentage eg 95%, 100% etc... however it is giving me 95.0%, 100.0% etc instead... with the extra ".0" behind...
I have tried various ways to convert this into an integer but was thrown an error... "Cannot dereference double"...
Is there a way to remove the decimal place??
Thanks!!!
[603 byte] By [
blurboiboi] at [2007-11-11 7:36:44]

# 1 Re: Problem with converting Double into Integer.... help needed
Look at this Sample program
import java.lang.Math.*;
public class Sample
{
public static void main(String args[])
{
int c=0;
try
{
Double d=(950.0/1000.0)*100;
c=d.intValue();
}catch(Exception e){}
System.out.println(c);
}
}
You cannot assign an int value(that is 100 or 500) to a double. It should be 100.0
# 2 Re: Problem with converting Double into Integer.... help needed
Look at this Sample program
import java.lang.Math.*;
public class Sample
{
public static void main(String args[])
{
int c=0;
try
{
Double d=(950.0/1000.0)*100;
c=d.intValue();
}catch(Exception e){}
System.out.println(c);
}
}
You cannot assign an int value(that is 100 or 500) to a double. It should be 100.0
ohz...so does it mean it cannot be done?... converting a double to a an integer... without the ".0" behind?
# 3 Re: Problem with converting Double into Integer.... help needed
just cast it...
double d = 100.0;
int i = (int) d;
This will truncate the decimal though, it will not round it. If you have 56.9, and cast it to an int:
int myInt = (int) 56.9;
myInt will be 56, not 57. there is a round function in the Math class if you need to round it.
int myInt = Math.round(56.9);
in this case, myInt will be 57.
destin at 2007-11-11 22:39:53 >

# 4 Re: Problem with converting Double into Integer.... help needed
hi destin,
I haf tried the casting mtd... it just gave me the "Cannot dereference Double" exception"... have not tried the Math.round way yet... will try it later at work later to see whether it works... thanks!
# 5 Re: Problem with converting Double into Integer.... help needed
Um... are you sure you're casting correctly? Show your code when you get the error.
Another way you could do it is using the DecimalFormat class.
DecimalFormat form = new DecimalFormat("0");
double myDouble = 7858.67676;
System.out.println(form.format(myDouble));
This will print:
7858
destin at 2007-11-11 22:41:56 >

# 6 Re: Problem with converting Double into Integer.... help needed
Try This
public class sample
{
public static void main(String args[])
{
int a=568,b=600;
double c,d,e=0.0;
c=(double)a;
d=(double)b;
try
{
e=(c/d)*100;
}catch(Exception e1){}
System.out.println(e);
}
}
That means first convert the integers to double individually before doing the operation. That will work fine
# 7 Re: Problem with converting Double into Integer.... help needed
hi destin,
I haf tried the casting mtd... it just gave me the "Cannot dereference Double" exception"... have not tried the Math.round way yet... will try it later at work later to see whether it works... thanks!
Be careful with Math.round.
It rounds the numbers the same way all of the time
eg 16.5 -> 17; 17.5 -> 18
When rounding, you should always round to either an even number or an odd number.
eg 16.5 -> 16; 17.5 -> 18
.
Try-at-home exercise:
Create large list of numbers
Calculate average
Round all numbers with Math.round
Calculate average
Round all numbers with the second method
Calculate average
Compare averages.
masher at 2007-11-11 22:43:59 >

# 8 Re: Problem with converting Double into Integer.... help needed
new Double(95.0).intValue();