To truncate in Java do this:
Math.floor(number * 100) / 100
.Math.floor(number * 100) / 100
truncates to 2 decimal places. Replace 100
with other multiples of 10
to truncate to less or more decimal places:
double aNumber = 643.7428694123;
Math.floor(aNumber * 10) / 10; // 643.7
Math.floor(aNumber * 100) / 100; // 643.74
Math.floor(aNumber * 1000) / 1000; // 643.742
Math.floor(aNumber * 10000) / 10000; // 643.7428
Math.floor(aNumber * 100000) / 100000; // 643.74286