DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: division

  1. #1
    Join Date
    May 2005


    I'm using this:
    NumberFormat formatter = new DecimalFormat("##.##");
    double nota=(punctaj / nr_intrebari)*10;
    String notafinala=formatter.format(nota);
    where punctaj and nr_intrebari are int

    Why nota or notafinala is 0 (zero) is the result of the division is not an integer?
    For example, if punctaj=1 and nr_intrebari=3 then nota=0.0.Why? I want the result to be a double like 0,33.

  2. #2
    Join Date
    Jul 2005
    Your trouble is with this line:
    double nota=(punctaj / nr_intrebari)*10;
    Because punctaj and nr_intrebari are int's, then 1/3 = 0 -> you're doing integer division. Integer division discards the remainder. 1/3 = 0.333... so the integer answer is 0. 6/5 = 1.2 so the integer answer is 1.

    If you want the result as a double, you have to cast.

    Try this:
    double nota=(punctaj / (double)nr_intrebari)*10;
    The (double) tells the compiler that you want to evaluate the integer division as if it were floating point division.
    Last edited by masher; 05-31-2006 at 08:08 PM.

Similar Threads

  1. Integer Division
    By yahshi in forum .NET
    Replies: 1
    Last Post: 03-20-2006, 01:42 AM
  2. Division by zero error handling
    By David Lemire in forum Database
    Replies: 2
    Last Post: 06-09-2005, 04:22 PM
  3. Division is resizing.
    By Shawn in forum Web
    Replies: 1
    Last Post: 05-02-2002, 12:59 PM
  4. Replies: 1
    Last Post: 01-09-2002, 06:42 AM
  5. Remainder of Integer Division
    By James Knowlton in forum .NET
    Replies: 2
    Last Post: 04-15-2001, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.