'How to truncate double to N decimal places?

I want to round my double value down to N decimal places (say, one), esentially just leaving out all the digits that follow:

0.123 #=> 0.1
0.19  #=> 0.1
0.2   #=> 0.2

This question has been brought up numerous times, for example here and here. The recommended approach is to use BigDecimal and then scale it, in particular to avoid expensive converting to string and back. The rounding mode I need is apparently RoundingMode.DOWN.

So the method is something like this:

static double truncate(double value, int places) {
    return new BigDecimal(value)
        .setScale(places, RoundingMode.DOWN)
        .doubleValue();
}

But, due to the loss of precision, it returns somewhat unexpected results:

truncate(0.2, 1) #=> 0.2
truncate(0.3, 1) #=> 0.2
truncate(0.4, 1) #=> 0.4

truncate(0.2, 3) #=> 0.2
truncate(0.3, 3) #=> 0.299
truncate(0.4, 3) #=> 0.4

This begs for two questions:

  1. Is it how it's supposed to work for 0.3? Why would there be a loss of precision in this case? Doesn't it defeat the whole purpose of having BigDecimal?

  2. How do I correctly truncate my values?

Thanks.



Solution 1:[1]

Multiply the number with pow(10,n) and store it in integer type & then again divide it by pow(10,n)

Solution 2:[2]

Truncating a double to N decimal places is not really a question that makes any sense, because simple decimal numbers (e.g. 0.3) cannot be represented as doubles.

If you want to know the true value of the double 0.3 you can do

System.out.println(new BigDecimal(0.3));

and you will see that it is really

0.299999999999999988897769753748434595763683319091796875

So 0.299 is the correct answer.

This question only really makes sense using BigDecimal and giving the answer as a BigDecimal or a String, not a double. @user1886323's answer shows how to do this.

Solution 3:[3]

I know this is an ancient post, but i just came up with a simple solution to it:

Have a second double which recieves the modulo of the number you are truncating, and then subtracts it from the first. Example (truncate to one digit)

    double original = 23.875114784205696;
    double truncater = original % 0.1;
    //truncater now equals 0.075114784205696;
    original - truncater = 23.8;

Obviously this can be expanded to whatever truncation is needed, and while it presumably does not shrink the double in memory allocation, it will cut off the end for display purposes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Priyanshu Jha
Solution 2
Solution 3 kahlzun