Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default What is the fastest way to chop off digits to the right of the decimal point?

    I've just learned about the BigDecimal Java class in my textbook, but it also says that BigDecimal is too slow to be acceptable for use in games or scientific applications that need to make thousands of floating-point calculations per second. So, I want to know a faster way. My question is explained in lines 17 to 19 in the following code:

    Java Code:
    package RoundingErrors;
    import java.text.NumberFormat;
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    
    public class RoundingErrors {
    	public static void main(String[] args){
    		
    		//Fixing a rounding error WITHOUT using BigDecimal objects:
    		System.out.println("How rounding works WITHOUT using BigDecimal:\n");
    		double subtotal = 100.05;//100.050
    		double discountPercent = .1;
    		double discountAmount = subtotal * discountPercent;//.1 * 100.05 = 10.005
    		double totalBeforeTax = subtotal - discountAmount;//100.050 - 10.005 = 90.045  
    		System.out.println("totalBeforeTax = " + subtotal + " - " + discountAmount + " = " + totalBeforeTax);
    		
    		//What is the simplest syntax for just chopping off the 5, and thus instead having 90.04?
    		//In other words, what is the fastest way to chop off all digits to the right of the first
    		//two digits after the decimal point?
    		
    		//get the objects for currency and percent formatting:
    		NumberFormat currency = NumberFormat.getCurrencyInstance();
    		NumberFormat percent = NumberFormat.getPercentInstance();
    		
    		//Format the message and print it to the console:
    		String formattedMessage = 
    			"Subtotal:			" + currency.format(subtotal) + "\n"
    		  + "Discount percent:  " + percent.format(discountPercent) + "\n"
    		  + "Discount amount:   " + currency.format(discountAmount) + "\n"//10.005, rounded to 10.01
    		  + "Total before tax:  " + currency.format(totalBeforeTax) + "\n";//90.045, rounded to 90.05
    		System.out.println(formattedMessage);
    		
    		//Fixing the rounding error with BigDecimal objects:
    		System.out.println("How rounding works using BigDecimal:\n");
    		BigDecimal subtotal2 = new BigDecimal("100.05");//100.05
    		BigDecimal discountPercent2 = new BigDecimal(".1");
    		BigDecimal discountAmount2 = subtotal2.multiply(discountPercent2);//10.005
    		discountAmount2 = discountAmount2.setScale(2, RoundingMode.HALF_UP);//make 10.005 = 10.01
    		BigDecimal totalBeforeTax2 = subtotal2.subtract(discountAmount2);//90.04
    		
    		//Format the message and print it to the console:
    		String formattedMessage2 = 
    			"Subtotal:			" + currency.format(subtotal2) + "\n"
    		  + "Discount percent:  " + percent.format(discountPercent2) + "\n"
    		  + "Discount amount:   " + currency.format(discountAmount2) + "\n"//10.005, rounded to 10.01
    		  + "Total before tax:  " + currency.format(totalBeforeTax2) + "\n";//90.045, rounded to 90.05
    		System.out.println(formattedMessage2);
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: What is the fastest way to chop off digits to the right of the decimal point?

    One way to remove the righthand digits is to multiply, cast to int and then divide.
    Eg 10.521 * 10 = 105.21, cast to int gives 105, divide by 10 gives 10.5
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: What is the fastest way to chop off digits to the right of the decimal point?

    If you are working with currency it is usually best to work with integers of the smallest denomination. So in the US,
    $10.50 would be 1050 cents. I would continue working with cents until time to display the values.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 05-29-2013, 05:38 AM
  2. Replies: 3
    Last Post: 10-27-2011, 05:23 PM
  3. Need Lowest and Highest decimal point for 0.73 and 0.76
    By maran.ramar in forum New To Java
    Replies: 5
    Last Post: 11-15-2010, 01:58 AM
  4. Replies: 4
    Last Post: 01-16-2009, 06:17 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
  •