# Thread: What is the fastest way to chop off digits to the right of the decimal point?

1. Member 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 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);
}
}```  Reply With Quote

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.5  Reply With Quote

3. Senior 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,
Jim  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•