# 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

## How can I truncate a double to only two decimal places in Java?

For example I have the variable 3.545555555, which I would want to truncate to just 3.54. Answer If you want that for display purposes, use java.text.DecimalFormat: If you need it for calculations, use java.lang.Math:

## How to change the decimal separator of DecimalFormat from comma to dot/point?

I have this little crazy method that converts BigDecimal values into nice and readable Strings. private String formatBigDecimal(BigDecimal bd){ DecimalFormat df = new DecimalFormat(); df….

## Show padding zeros using DecimalFormat

I’m using DecimalFormat to format doubles to 2 decimal places like this: DecimalFormat dec = new DecimalFormat(“#.##”); double rawPercent = ( (double)(count.getCount().intValue()) / …