-1

How do i change 2.278481... to 2.2?

double a = 180;
double b = 79;
double x = (a/b);

Math.ceil() returns 3 and Math.floor() returns 2

ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
zorer
  • 13
  • 2

1 Answers1

3

You can just set the setMaximumFractionDigits to 1. Like this:

public class Test {

public static void main(String[] args) {
    System.out.println(format(14.0184849945)); // prints '14.0'
    System.out.println(format(13)); // prints '13'
    System.out.println(format(3.5)); // prints '3.5'
    System.out.println(format(3.138136)); // prints '3.1'
}

 public static String format(Number n) {
    NumberFormat format = DecimalFormat.getInstance();
    format.setRoundingMode(RoundingMode.FLOOR);
    format.setMinimumFractionDigits(0);
    format.setMaximumFractionDigits(1);
    return format.format(n);
 }

}

This may helps you

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48