# Java Data Type How to - Round a number with half down, floor, ceiling

## Question

We would like to know how to round a number with half down, floor, ceiling.

## Answer

```import java.math.RoundingMode;
import java.text.NumberFormat;
//from w w w  .  jav  a  2  s. c o  m
public class NumberFormatRounding {
public static void main(String[] args) {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
nf.setRoundingMode (RoundingMode.HALF_DOWN);
System.out.println("Default rounding mode: " + nf.getRoundingMode());
System.out.println("123.454 rounds to " + nf.format(123.454));
System.out.println("123.455 rounds to " + nf.format(123.455));
System.out.println("123.456 rounds to " + nf.format(123.456));
System.out.println();

nf.setRoundingMode (RoundingMode.FLOOR);
System.out.println("Default rounding mode: " + nf.getRoundingMode());
System.out.println("123.454 rounds to " + nf.format(123.454));
System.out.println("123.455 rounds to " + nf.format(123.455));
System.out.println("123.456 rounds to " + nf.format(123.456));
System.out.println();

nf.setMaximumFractionDigits(2);
nf.setRoundingMode (RoundingMode.CEILING);
System.out.println("Default rounding mode: " + nf.getRoundingMode());
System.out.println("123.454 rounds to " + nf.format(123.454));
System.out.println("123.455 rounds to " + nf.format(123.455));
System.out.println("123.456 rounds to " + nf.format(123.456));
System.out.println();

}
}
```

The code above generates the following result.