Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to multiply INT and DOUBLE....

variable type

  • Please log in to reply
7 replies to this topic

#1 yourmom615

yourmom615

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 21 March 2011 - 06:01 PM

So the last part of this wannabee java, I am trying to multiple INT and DOUBLE, due to the fact that commission is base off of percentages......how do I do this when all variable type match and the IDE is telling me a possible loss of precision...any suggestion....?????
import java.util.Scanner;
public class Project2 {

    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        
        int HE = 1, PE = 2, SA = 3;
        int employeeType = 0;
        int weeklySal= 0,hoursWorked = 0;//Hourly-based Employees (HE)
        int salaryDue =0;
        int numberPieces = 0,wage_Per_peice = 0; //For Piece-based Employees(PE)           
        double items_sold = 0, baseSalary = 0;
        double employeeCommission = 0,commissionDue = 0;
         
         
        System.out.println("Employee Type: HE (1), PE (2), SA (3)");
        employeeType = input.nextInt();
               
        while ( employeeType == 1){
          System.out.println("Please enter # hours worked: ");
        hoursWorked = input.nextInt();
        if ( hoursWorked > 70){
          System.out.println("Hours need be less than 71 weekly: ");
         }
        System.out.println("Please enter hourly pay: ");
        weeklySal = input.nextInt();
        salaryDue = weeklySal * hoursWorked;
        System.out.println("Weeks Pay: " + salaryDue);
        System.exit( 0 );
        }
        
        while ( employeeType == 2){
        System.out.println("Please enter # of pieces produced: ");
        numberPieces = input.nextInt();
                
        System.out.println("Please wage per piece: ");
        wage_Per_peice = input.nextInt();
                                    
        salaryDue = numberPieces * wage_Per_peice;
        System.out.println("Weeks Pay: " + salaryDue);
        System.exit( 0 );
        }
          
        while ( employeeType == 3){
        System.out.println("Please enter # items sold: ");
        items_sold = input.nextDouble();
        
        System.out.println("Please weekly base salary: ");
        baseSalary = input.nextDouble();
        
        System.out.println("Please enter your commision: ");
        employeeCommission= input.nextDouble();
        [COLOR="red"]salaryDue = items_sold * baseSalary;[/COLOR]        
        commissionDue = salaryDue * employeeCommission;
        
        System.out.println("Weeks Pay: " + commissionDue);
        System.exit( 0 );
        }   
    }
}

Red is the line in question.....

Edited by yourmom615, 21 March 2011 - 06:32 PM.
New Issue

  • 0

#2 yourmom615

yourmom615

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 21 March 2011 - 06:09 PM

See Above!!!!!!!!!!!
  • 0

#3 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 21 March 2011 - 07:37 PM

I'd change salaryDue to double instead of int.
  • 0

#4 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 21 March 2011 - 07:53 PM

The loss of precision is because salaryDue is an int, and when you multiply two doubles (which items_sold and baseSalary both are) this results in a double. Since slarayDue is an int, assigning it a double will result in a loss of precision. Because Java likes to harass you over BS, you have to be explicit:
salaryDue = (int) (items_sold * baseSalary);

As a question to your interface logic, how is it that an employees base salary is dependent on the number of items that employee sold? This insinuates a purely commission-based employment system (for example, car salesman), but you then multiply that value by the employeeCommission value (Which is cryptically asked just as "Please enter your commission", which could mean a great deal of different things), so I'm not sure how this works exactly. Are you certain you have the program logic right for what you intend?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 Mozana

Mozana

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts
  • Programming Language:Java, C#, PHP, JavaScript, PL/SQL
  • Learning:Objective-C

Posted 22 March 2011 - 07:56 AM

I think the best practise is to make salaryDue double beecause it is currency, one other thing int don't take type double but double does take int and thus wil not result in the problem;
  • 0

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 22 March 2011 - 09:20 AM

I would use integers and work with the money as if it were cents, meaning 1.00$ would be an integer of value 100.
When you need to display it, divide by 100 or just process as String and throw in a comma before the last 2 characters.
integers are easier as they have no floating point issues.
if( doubleVariable== 0.0 )
Will rarely be true, even if you think it contains 0. In reality it propably contains like 0.000000000000019 or so.
  • 0

#7 eafkuor

eafkuor

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 214 posts

Posted 22 March 2011 - 11:53 AM

Or, if you find it easier, you could use two different variables: one for the integer part, one for the decimal part.
  • 0

#8 yourmom615

yourmom615

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 22 March 2011 - 03:52 PM

Thanks for the info....I was able to manipulate it later and found that using double is better due to the currency issue.
As far as, the commission part on the last employee type, I got clarification as to what actually needed done and the commission will be based off a percentage of the items sold * the item price and then adding the base weekly salary into it. I have added the code below to show what I mean. Any critiques are welcome. Thanks for the kind words and advice. Hats off to you!!!!
while ( employeeType == 3){ 
        if ( week_Number > 0 && week_Number <= 53){     
        System.out.println("Please enter number between 1-52: " );
        week_Number =input.nextInt();        
       }
        System.out.println("Please enter number of items sold: ");
        items_sold = input.nextDouble();
        
        System.out.println("Please enter price of each item: ");
        item_Price = input.nextDouble();
        
        System.out.println("Please weekly base salary: ");
        baseweekly_Salary = input.nextDouble();
        
        System.out.println("Please enter your commision: ");
        employeeCommission= input.nextDouble();
        salaryDue = (items_sold * item_Price);
        final_SA_wages = (salaryDue *employeeCommission)+ baseweekly_Salary;
              
        
        System.out.println("Weeks Pay: " + commissionDue);
        System.out.println("ABC Company");
        System.out.println("=============================");
        System.out.println("Week #:" + week_Number);
        System.out.println("@@@@@@@");
        System.out.println("Employee type:" + employeeType );
        System.out.println("@@@@@@");
        System.out.println("Weekly Base Salary:" +baseweekly_Salary) ;
        System.out.println("Number of items sold:" + items_sold  );
        System.out.println("Salary Due:" + final_SA_wages);
        System.out.println("=============================");
        
        System.exit( 0 );
        }  
This is still a work in progress so be easy....please!
  • 0





Also tagged with one or more of these keywords: variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download