Java Algorithms How to - Save decimal








Question

We would like to know how to save decimal.

Answer

import java.math.BigDecimal;
import java.math.RoundingMode;
/*  w  w  w .  j av a2 s  . c  o  m*/
public class Main {
  public static void main(String[] args) {
    double operation = 890.0 / 1440.0;
    BigDecimal big = new BigDecimal(operation);
    big = big.setScale(4, RoundingMode.HALF_UP);
    double d2 = big.doubleValue();
    System.out.println(String.format("operation : %s", operation));
    System.out.println(String.format("scaled : %s", d2));
  }
}

The code above generates the following result.