Answers for "java round to decimals"

6

java round double to 2 decimals

double roundOff = Math.round(a * 100.0) / 100.0;
// Or
double roundOff = (double) Math.round(a * 100) / 100;
// both have the same output.
Posted by: Guest on September-21-2021
0

round decimals in java string

package com.mkyong.math.rounding;

public class StringFormatExample {

  public static void main(String[] args) {

      double input = 1205.6358;

      System.out.println("salary : " + input);

      // round half-up, no way control
      // 1205.64
      System.out.println("salary : " + String.format("%.2f", input));

      // 1205.64
      System.out.format("salary : %.2f", input);

  }

}
Posted by: Guest on March-06-2022

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language