Precision Handling in Java

JavaServer Side ProgrammingProgramming

Let us see how precisions are handled in Java −

Example

 Live Demo

import java.io.*;
import java.lang.*;
public class Demo{
   public static void main(String[] args){
      double my_val = 34.909;
      System.out.println("The formatted value of 34.909 is ");
      System.out.println(String.format("%.7f", my_val));
      double my_val_2 = 12.56;
      System.out.println("The formatted value of 12.56 is ");
      System.out.println(String.format("%.9f", my_val_2));
   }
}

Output

The formatted value of 34.909 is
34.9090000
The formatted value of 12.56 is
12.560000000

A class named Demo contains the main function, where a double valued integer is declared, and it is formatted by specifying the number that it should be formatted to. Similarly, another double variable is defined, and formatted and printed on the screen.

raja
Published on 17-Aug-2020 09:46:20
Advertisements