Go Back   PCMech Forums > Software > Web Development & Hosting

Web Development & Hosting Questions about HTML, CSS, Javascript, Java, MySQL, PHP, Perl, general programming, and Web Hosting. Want to know what others think of your site? Ask here..

Recommended: Click Here to Run a Free Scan for PC errors

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 10-14-2004, 08:53 PM   #1
racerxfactor
Member (8 bit)
 
Join Date: Jun 2001
Posts: 226
Send a message via ICQ to racerxfactor Send a message via AIM to racerxfactor
Java question: How do you multiply in decimal

I'm trying to set a variable and mulitplying it by 1.5. I can multiply it with 2 easily but when I do 1.5 it runs into problems

int orate = (otime * trate)* 2 ;
racerxfactor is offline   Reply With Quote
Old 10-14-2004, 09:21 PM   #2
colecifer
Member (9 bit)
 
colecifer's Avatar
 
Join Date: Jan 2004
Location: Kansas City(westwood), KS
Posts: 467
are you geting a decimal answer? If so that is your problem. The integer data type is only integers. Declare orate as a double and you'll be set.
Here is how it should look
double orate = (otime * trate) * 2;
__________________
colecifer is offline   Reply With Quote
Old 10-16-2004, 06:05 PM   #3
raja
Member (7 bit)
 
raja's Avatar
 
Join Date: Oct 2001
Location: Macon, Georgia
Posts: 99
Send a message via ICQ to raja Send a message via AIM to raja
what colecifer said is definitely your best bet. however, if "orate" has to be of type int, and what you want is to multiply "(otime * trate)" by 1.5 and then round the result, then you'll need to use:
Quote:
int orate = Math.round((otime * trate) * 1.5)
One of these two options (mine or colecifer's) should work, depending on what exactly you're trying to accomplish with your program. What's your assignment? Could we see your code so far?

Edit: I made a stupid suggestion about type casting originally, but Java automatically will convert int values to a double for a mathematical operation. The way your code was as you originally posted it, Java would multiply (otime * trate) * 1.5 and then truncate the result. So if you got 2.9, then the value assigned to orate would be 2.

Last edited by raja; 10-16-2004 at 06:10 PM..
raja is offline   Reply With Quote
Old 10-16-2004, 06:14 PM   #4
racerxfactor
Member (8 bit)
 
Join Date: Jun 2001
Posts: 226
Send a message via ICQ to racerxfactor Send a message via AIM to racerxfactor
The program I am writing is suppose to record the net salary for an employee. The user inputs their id, hours worked, rates per hour etc, and it's suppose to come up with their net salary minus tax deductions etc.

import java.io.* ;



class Empsal {

public static void main(String args[])throws IOException

{

InputStreamReader istream = new InputStreamReader(System.in) ;

BufferedReader bufRead = new BufferedReader(istream) ;


System.out.println("==============================================");
System.out.println("Welcome To The Employee Salary Record Program");

System.out.println("==============================================");




System.out.println("Please Enter In Your Employeee Id number: ");

String empid = bufRead.readLine();


System.out.println("Enter your first name: ");

String firstname = bufRead.readLine();


System.out.println("Enter your last name: ");

String lastname = bufRead.readLine();


System.out.println("Please Enter How Many Hours You've Worked: ");

String workhour = bufRead.readLine();
int hwork = Integer.parseInt(workhour);

System.out.println("Please Enter In Your Rate per Hour: ");

String thisrate = bufRead.readLine();
int trate = Integer.parseInt(thisrate);



int gross = hwork * trate;
int otime = hwork - 40;
double orate = (otime * trate)* 1.5;
double taxr = gross * .10;
double nets = gross + orate - taxr;


/**1. Employee ID
* 2. Employee Name
* 3. Number of hours worked
* 4. Rate per hour
* 5. Tax rate
*/

System.out.println("============================================");
System.out.println("Employee ID: " + empid);
System.out.println("First: " + firstname);
System.out.println("Last: " + lastname);
System.out.println("Hours Worked " + workhour);
System.out.println("Rate Per Hour " + thisrate);

if (hwork>40)
{
System.out.println("Your Overtime Hours: " + otime);
}

System.out.println("Your Overtime Pay: " + orate);
System.out.println("Your gross Salary is " + gross);
System.out.println("Current Tax Deduction 10%: Your Deduction is " +taxr);
System.out.println("Your Total Net Salary is: " + nets);
System.out.println("===========================================");






}
}
racerxfactor is offline   Reply With Quote
Old 10-16-2004, 06:16 PM   #5
racerxfactor
Member (8 bit)
 
Join Date: Jun 2001
Posts: 226
Send a message via ICQ to racerxfactor Send a message via AIM to racerxfactor
The next steps would be error checking and switch statements
racerxfactor is offline   Reply With Quote
Old 10-16-2004, 06:34 PM   #6
raja
Member (7 bit)
 
raja's Avatar
 
Join Date: Oct 2001
Location: Macon, Georgia
Posts: 99
Send a message via ICQ to raja Send a message via AIM to raja
that looks good, but here are a few suggestions - use them or don't, it's up to you.

1) For hourly rate, I'd make it of type double. Not everyone works for exactly a whole dollar value per hour. You could do the same thing with hours worked, if you wanted. However, using type double for either of these means you'll have to change your "gross" variable to type double, also. If you use a double for hours worked, you'll also have to change the overtime (otime) variable to a double.

2) If you want to make sure that your double variable results print out in "X.XX" format (i.e. so you always have exactly two digits after the decimal), then an easy method to use is this:
Quote:
public static String doubleFormat(double d)
{
DecimalFormat myFormatter = new DecimalFormat("#0.00");
String formatted = myFormatter.format(d);
return formatted;
}
Just add the doubleFormat method after your main method, and then where you want to print your double value, just include a call to the doubleFormat method with your result variable as a parameter (e.g. doubleFormat(nets)). Also, you'll have to import java.text.* at the top for this method to work. Good luck!
raja is offline   Reply With Quote
Old 10-19-2004, 12:02 AM   #7
racerxfactor
Member (8 bit)
 
Join Date: Jun 2001
Posts: 226
Send a message via ICQ to racerxfactor Send a message via AIM to racerxfactor
Through all of this I didn't realized there was another problem in my program. Now I've been able to seperate the over time hours from regular hours with this statement.

int = hwork - 40;

now I need to seperate regular hours from my overtime....
racerxfactor is offline   Reply With Quote
Old 10-19-2004, 02:23 AM   #8
raja
Member (7 bit)
 
raja's Avatar
 
Join Date: Oct 2001
Location: Macon, Georgia
Posts: 99
Send a message via ICQ to raja Send a message via AIM to raja
I'm not sure I understand what you mean. What "regular hours" are you extracting from your overtime hours variable? Your "regular hours" are already stored in the hwork variable.
raja is offline   Reply With Quote
Old 10-19-2004, 02:27 AM   #9
racerxfactor
Member (8 bit)
 
Join Date: Jun 2001
Posts: 226
Send a message via ICQ to racerxfactor Send a message via AIM to racerxfactor
ok, i just got the calculations for the regular hours after some major brain tease. thanks for that tip, i will try it out
racerxfactor is offline   Reply With Quote
Old 10-19-2004, 02:30 AM   #10
racerxfactor
Member (8 bit)
 
Join Date: Jun 2001
Posts: 226
Send a message via ICQ to racerxfactor Send a message via AIM to racerxfactor
well basiclly the problem was that when i was calculating the gross, it was including the overtime hours in that equation. so even if the user inputs 45 hours times 8 dollars an hour, it will seperate the 5 from the 45 to calculate the over times hours by 1.5 but still calculate the 45 hours as regular time.
racerxfactor is offline   Reply With Quote
Old 10-26-2004, 03:16 AM   #11
racerxfactor
Member (8 bit)
 
Join Date: Jun 2001
Posts: 226
Send a message via ICQ to racerxfactor Send a message via AIM to racerxfactor
switch statements and menu !!!!

Here's a menu I wrote for the program and I can't seem to get it to work. *gulp, help~!

import java.io.*;
public class ca
{
public static void main(String args[]) throws IOException
{
char cchoice;
/*
Main Menu
1. Input Employee Info.
2. Display Employee Info.
3. Display Regular Time Info.
4. Display Overtime Info.
5. Display Tax Info.
6. Display Gross and Net Salary.
7. Exit Program
*/

switch (cchoice)
{
case '1':
System.out.println("You selected 'Input Employee Info.'");
break;

case '2':
System.out.println("You selected 'Display Employee Ifno.'");
break;

case '3':
System.out.println("You selected 'Display Regular Time Info'.");
break;

case '4':
System.out.println("You selected 'Display Overtime Info'.");
break;

case '5':
System.out.println("You selected 'Display Tax Info'.");
break;

case '6':
System.out.println("You selected 'Display Gross and Net Salary'.");
break;

case '7':
System.out.println("You selected 'Exit Program'.");
break;

default:
System.out.println("Invalid selection.");
//break;
}
}
}
racerxfactor is offline   Reply With Quote
Old 10-26-2004, 10:05 AM   #12
raja
Member (7 bit)
 
raja's Avatar
 
Join Date: Oct 2001
Location: Macon, Georgia
Posts: 99
Send a message via ICQ to raja Send a message via AIM to raja
You currently don't have anything in there to make the program actually display the menu options and then prompt for a selection from the user!
raja is offline   Reply With Quote
Reply

Bookmarks

Follow PCMech
Subscribe

Free Weekly Newsletter. Sign up and receive our free report: 20 Tips For Becoming a Technology Power User.

NAME:
EMAIL:

Latest Posts
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -5. The time now is 06:48 AM.