1. ## Data type error

Hi,
The following is the code for finding the EMI calculation.This is working fine for 8 digit number but for 9 digit number
getting a exponent E at the end of the output .

else{

principal = Double.parseDouble(txtLoanAmount.getString());
term = Double.parseDouble(txtYears.getString());
interest = Double.parseDouble(txtInterestRate.getString())/ 1200;
year = term / 12;
clear();
calculateEmi();
display.setCurrent(new ResultCanvas());
}
public void calculateEmi(){
try{
math=1;
for(int i=0;i<term;i++) {
math = math*(1/(1 + interest));
}
math = principal * interest / (1 - math);
emi = Math.ceil(math);

System.out.println("emi"+emi);
} catch(Exception e){

}

}
Anamika

2. ## Re: Data type error

This is normal. You don't show enough code for us to see all the data types, but I'm assuming "emi" is double.

When you print a double, it is formated according to Double.toString()... take a look in the JavaDocs to see what this does.

If you are doing financial calculations, you should avoid "double". Double is not suitable for financial calculations, because the are inexact. That's right, "double" and "float" do not necessarily store exact numbers. Usually, the numbers they store are approximations. If you use them for financial calculations, you will have very strange errors.

For financial calculations, use integers (int or long). Integers are always precise. You will need to write some code to convert String input, and String output. For example:

"€135.21" -> 1352100

Note that I'm suggesting you work in 100ths of the smallest unit (so, 100ths of one penny or one cent). This will help to avoid compounding rounding errors, and is the usual working practice for financial calculations.

When converting back to a String, you will need to drop the last two digits and insert a ".". When you lose the last two digits, you will need to round the result. Use the following rules (Google for "banker's rounding"):

1. If the last two digits are 00 to 49, then just lose the digits (round down). 1012149 -> "€101.21"
2. If the last two are 51 to 99, then lose the digits, and add one (round up). 1012151 -> "€101.22"
3. if the last two are 50, then round to the nearest even number. 1012150 -> "€101.22", 1012650 -> "€101.26"

Graham.

#### Posting Permissions

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