Using precision for floating-point numbers and strings. - Java Language Basics

Java examples for Language Basics:Number Format

Description

Using precision for floating-point numbers and strings.

Demo Code

public class Main 
{
   public static void main(String[] args)
   { //from w  ww .j a v a  2 s  .  c  om
      double f = 123.94536; 
      String s = "Happy Birthday"; 

      System.out.printf("Using precision for floating-point numbers\n");
      System.out.printf("\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f);  
   
      System.out.printf("Using precision for strings\n");
      System.out.printf("\t%.11s\n", s);
   } 
}

Result


Related Tutorials