7 Replies - 11569 Views - Last Post: 24 January 2009 - 11:50 AM Rate Topic: -----

#1 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Rounding in Java

Posted 24 January 2009 - 07:30 AM

Hi, new to Java programming here, and I'm writing a program for class that asks us to round our answer to the nearest second decimal place. Meaning our answers should come back in dollar amounts. Here's a look at my code so you can follow my thought process:

/**
 * @(#)Sales.java
 *
 * Sales application
 *
 * @Brandon Matykunas
 * @version 1.00 2009/1/22
 */
import javax.swing.*;
 
public class Sales {
    
    public static void main(String[] args)
    {
    	String openMsg, printOutTitle, finalMessage,
				//Item One Questions
				item1Dialog, askItemNum1, askItemDesc1, askItemQuant1, askItemPrice1,
				//Item Two Questions
				item2Dialog, askItemNum2, askItemDesc2, askItemQuant2, askItemPrice2,
				//Item Three Questions
				item3Dialog, askItemNum3, askItemDesc3, askItemQuant3, askItemPrice3,
				//Item One Strings
				item1NumStr, item1DescStr, item1QuantStr, item1PriceStr,
				//Item Two Strings
				item2NumStr, item2DescStr, item2QuantStr, item2PriceStr,
				//Item Three Strings
				item3NumStr, item3DescStr, item3QuantStr, item3PriceStr,
				//Output Message for Item 1
				printItem1Num, printItem1Desc, printItem1QP,
				//Output Message for Item 2
				printItem2Num, printItem2Desc, printItem2QP,
				//Output Message for Item 3
				printItem3Num, printItem3Desc, printItem3QP,
				//Output for Subtotal, Sales Tax, and Grand Total
				printGrandSubTot, printSalesTax, printGrandTotal;
		int itemNum1, itemQuant1, itemNum2, itemQuant2, itemNum3, itemQuant3;
		double itemPrice1, item1SubTot, itemPrice2, item2SubTot, itemPrice3, item3SubTot, grandSubTot, salesTax, grandTotal;
    	final double TAX_RATE = 0.0625;
    	
    	//Begin Program. Welcome the user.
    	openMsg = "Welcome. Press OK to start next sale.";
    	JOptionPane.showMessageDialog(null, openMsg);
    	
    	//Ask about Item 1
    	item1Dialog = "For the first item, I'll need the item number, description, quantity to be purchased, and price. Enter when ready";
		JOptionPane.showMessageDialog(null, item1Dialog);
		//Get Item number
		askItemNum1 = "Please enter the item number for the first item:";
		item1NumStr = JOptionPane.showInputDialog ( askItemNum1 );
		itemNum1 = Integer.parseInt ( item1NumStr );
		//Get Item Description
		askItemDesc1 = "Please enter the item description for the first item:";
		item1DescStr = JOptionPane.showInputDialog ( askItemDesc1 );
		//Get Item Quantity
		askItemQuant1 = "Please tell me how many of the first item to ring up:";
		item1QuantStr = JOptionPane.showInputDialog( askItemQuant1 );
		itemQuant1 = Integer.parseInt ( item1QuantStr );
		//Get Item Price
		askItemPrice1 = "How much is item one:";
		item1PriceStr = JOptionPane.showInputDialog ( askItemPrice1 );
		itemPrice1 = Double.parseDouble ( item1PriceStr );
		
		//Calculate Item One subtotal
		item1SubTot = itemQuant1 * itemPrice1;
		
		//Ask about Item 2
		item2Dialog = "For the second item, I'll need the item number, description, quantity to be purchased, and price. Enter when ready";
		JOptionPane.showMessageDialog(null, item2Dialog);
		//Get Item number
		askItemNum2 = "Please enter the item number for the second item:";
		item2NumStr = JOptionPane.showInputDialog ( askItemNum2 );
		itemNum2 = Integer.parseInt ( item2NumStr );
		//Get Item Description
		askItemDesc2 = "Please enter the item description for the second item:";
		item2DescStr = JOptionPane.showInputDialog ( askItemDesc2 );
		//Get Item Quantity
		askItemQuant2 = "Please tell me how many of the second item to ring up:";
		item2QuantStr = JOptionPane.showInputDialog( askItemQuant2 );
		itemQuant2 = Integer.parseInt ( item2QuantStr );
		//Get Item Price
		askItemPrice2 = "How much is item two:";
		item2PriceStr = JOptionPane.showInputDialog ( askItemPrice2 );
		itemPrice2 = Double.parseDouble ( item2PriceStr );

		//Calculate Item Two Subtotal
		item2SubTot = itemQuant2 * itemPrice2;
		
		//Ask about Item 3
		item3Dialog = "For the third item, I'll need the item number, description, quantity to be purchased, and price. Enter when ready";
		JOptionPane.showMessageDialog(null, item3Dialog);
		//Get Item number
		askItemNum3 = "Please enter the item number for the third item:";
		item3NumStr = JOptionPane.showInputDialog ( askItemNum3 );
		itemNum3 = Integer.parseInt ( item3NumStr );
		//Get Item Description
		askItemDesc3 = "Please enter the item description for the third item:";
		item3DescStr = JOptionPane.showInputDialog ( askItemDesc3 );
		//Get Item Quantity
		askItemQuant3 = "Please tell me how many of the third item to ring up:";
		item3QuantStr = JOptionPane.showInputDialog( askItemQuant3 );
		itemQuant3 = Integer.parseInt ( item3QuantStr );
		//Get Item Price
		askItemPrice3 = "How much is item three:";
		item3PriceStr = JOptionPane.showInputDialog ( askItemPrice3 );
		itemPrice3 = Double.parseDouble ( item3PriceStr );

		//Calculate Item Three Subtotal
		item3SubTot = itemQuant3 * itemPrice3;
		
		//Final Calculations Subtotal, Sales Tax, Grand Total
		grandSubTot = item1SubTot + item2SubTot + item3SubTot;
		salesTax = grandSubTot * TAX_RATE;
		grandTotal = grandSubTot + salesTax;
		
		//Prepare Receipt
		printOutTitle = "Thanks for Shopping.Here's your receipt.\n";
		printItem1Desc = item1DescStr + "\n";
		printItem1Num = "Item Number:" + "\t" + itemNum1 +"\n";
		printItem1QP = itemQuant1 + "\t" + "@" + "\t" + itemPrice1 + "\n";
		printItem2Desc = item2DescStr + "\n";
		printItem2Num = "Item Number:" + "\t" + itemNum2 +"\n";
		printItem2QP = itemQuant2 + "\t" + "@" + "\t" + itemPrice2 + "\n";
		printItem3Desc = item3DescStr + "\n";
		printItem3Num = "Item Number:" + "\t" + itemNum3 +"\n";
		printItem3QP = itemQuant3 + "\t" + "@" + "\t" + itemPrice3 + "\n";
		printGrandSubTot = "\t" + grandSubTot + "\n";
		printSalesTax = "\t" + salesTax + "\n";
		printGrandTotal = "\t" + grandTotal + "\n";
		
		finalMessage = printOutTitle + printItem1Desc + printItem1Num + printItem1QP
						 + printItem2Desc + printItem2Num + printItem2QP
			 			 + printItem3Desc + printItem3Num + printItem3QP
			 			 + printGrandSubTot + printSalesTax + printGrandTotal;
		
		JOptionPane.showMessageDialog ( null, finalMessage );
    	
    	System.exit(0);
    }
    
}



So to get the last two outputs to round I am thinking that I need to do something like this:

/**
 * @(#)Sales.java
 *
 * Sales application
 *
 * @Brandon Matykunas
 * @version 1.00 2009/1/22
 */
import javax.swing.*;
//Add the math class here
import java.lang.enum //I'm not so sure about this
 
public class Sales {
    
    public static void main(String[] args)



And I think I would need to create two more variables one for sales tax rounded, and one for Grand Total rounded, and output those instead of the variables I'm outputting right now. But to do this I think I need to do it like this:

salesTaxRound = RoundingMode HALF_UP(salesTax);
grandTotalRound = RoundingMode HALF_UP(grandTotal);



Again I really have no idea of the proper syntax, or if I'm even anywhere close to what I need to be doing, but feel free to point and laugh as long as afterwards you point out the correct way to do it, or can point me to an example to closest resembles what I'm trying to do. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Rounding in Java

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Rounding in Java

Posted 24 January 2009 - 07:43 AM

Multiply your value by 100, Apply the Math.round(double newValue) to your new value and then divide the result by 100.

double d = 45.6789;
double newValue = d*100;
d = Math.round(newValue) / 100;
Of course you don't need the intermediary variable newValue

This post has been edited by Gloin: 24 January 2009 - 07:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Rounding in Java

Posted 24 January 2009 - 07:51 AM

Ok I not 100% sure I follow. so let me use your idea and apply it to my code and you can tell me if I'm right or wrong

double salesTaxRound = salesTax * 100;
salesTax = Math.round(salesTaxRound)/100;

and then print the value of salesTaxRound right?

Also do I need to import another file like a math class or can I just run it with just the
import javax.swing.*;
Was This Post Helpful? 0
  • +
  • -

#4 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Rounding in Java

Posted 24 January 2009 - 07:53 AM

hello webmin, here is a way to round your double:
The basics of this method is to multiply the double by the nearest number you would like to round to(e.g. nearest 10th multiply by 10), then use the Math.round() method to round it and remove numbers after the decimal point, after the number is rounded you divide it by 10(nearest 10th), and then you will get your number rounded to the nearest tenth. here is the code that demonstrates what i explained:
class Rounding{
	public static void main(String[] args){
		
double myDouble = 2.456;//the initial decimal

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

double myDoubleMultiplied = myDouble * 10;//the decimal is multiplied by 10 to obtain 24.56(so we get to the nearest second place)

System.out.println("myDoubleMultipled = " + myDoubleMultiplied);

double myDoubleRounded = Math.round(myDoubleMultiplied);///the Math.round() method is used to remove the numbers after the decimal point

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

double myDoubleDivided = myDoubleRounded / 10.0;//Finally, the number is again divided by 10 to get the initial number rounded to the nearest 10th i.e. 2.5

System.out.println("myDoubleDivided = " + myDoubleDivided);
}
}


I made it as a class so you can test it by yourself and learn what each part of the code is used for, i hope this helps, good luck ;)

This post has been edited by ayman_mastermind: 24 January 2009 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

#5 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Rounding in Java

Posted 24 January 2009 - 08:11 AM

You need to import java.lang.math for this example but it's very simple to do without it as well..
Was This Post Helpful? 0
  • +
  • -

#6 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Rounding in Java

Posted 24 January 2009 - 09:02 AM

Check this out if you don't want to include the java.lang.math

double d = 45.1789;
double e = 45.6739;
d = (double) ((int) ((d + 0.005) * 100) % Integer.MAX_VALUE) / 100;
e = (double) ((int) ((e + 0.005) * 100) % Integer.MAX_VALUE) / 100;

System.out.println(d + ", " + e);

This post has been edited by Gloin: 24 January 2009 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#7 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Rounding in Java

Posted 24 January 2009 - 10:27 AM

Math does not need to be imported it can already be used without need for importing
Was This Post Helpful? 0
  • +
  • -

#8 ProGraM  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 150
  • Joined: 28-September 05

Re: Rounding in Java

Posted 24 January 2009 - 11:50 AM

Easiest way to do it right here D:

import java.text.DecimalFormat;

DecimalFormat  fmt = new DecimalFormat("0.##");




Then here's an Example output

System.out.println("Round Price: $" + fmt.format(myDouble));



hope this helps :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1