Skip to content

Tag: decimalformat

How to round and scale a number using DecimalFormat

I’m trying to to format a number using the DecimalFormat but the problem that I didn’t get the expected result. Here is the problem: I have this number: 1439131519 and I want to print only the five first digits but with a comma after 4 digits like this: 1439,1. I have tried to use DecimalFormat but it didn’t work. I

Rounding with DecimalFormat in Java

Let’s look at the following statements in Java. In the above statements, all other cases are obvious except the following. It should return 3 but it returns 2. How? Answer This is intentional behavior. From the documentation: Rounding DecimalFormat uses half-even rounding (see ROUND_HALF_EVEN) for formatting. About ROUND_HALF_EVEN: Rounding mode to round towards the “nearest neighbor” unless both neighbors are