×

Discussion Board

Results 1 to 2 of 2

Thread: Data type error

  1. #1
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    78

    Post 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){

    }

    }
    Thanks in advance
    Anamika

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    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.

Similar Threads

  1. S60 Data Type Registry / Generic Data Recognizer
    By lenclud in forum News and Announcements
    Replies: 3
    Last Post: 2012-08-22, 21:17
  2. help on symbai C++ data type
    By CLX21 in forum Symbian
    Replies: 4
    Last Post: 2009-06-11, 10:41
  3. comparing data type
    By Lawren in forum Symbian
    Replies: 7
    Last Post: 2007-04-16, 11:25
  4. what data type can i use
    By nitinp_bioinf in forum Symbian
    Replies: 1
    Last Post: 2004-10-10, 14:28
  5. How to change data type?
    By helloeveryone in forum Bluetooth Technology
    Replies: 0
    Last Post: 2003-08-13, 07:22

Posting Permissions

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