Results 1 to 3 of 3
 06242016, 02:45 AM #1Member
 Join Date
 Mar 2016
 Posts
 93
 Rep Power
 0
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 floatingpoint 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); } }
 06242016, 03:02 AM #2
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.5If you don't understand my response, don't ignore it, ask a question.
 06242016, 03:30 AM #3Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Problem using split to put two sides of a decimal point in an array
By Aaron5466 in forum New To JavaReplies: 2Last Post: 05292013, 05:38 AM 
Java help 25element array of digits to store integers as large as 25 digits
By CCC3 in forum New To JavaReplies: 3Last Post: 10272011, 05:23 PM 
Need Lowest and Highest decimal point for 0.73 and 0.76
By maran.ramar in forum New To JavaReplies: 5Last Post: 11152010, 01:58 AM 
round down to 2 sig. numbers after the decimal point
By soc86 in forum New To JavaReplies: 4Last Post: 01162009, 06:17 PM
Bookmarks