The Artima Developer Community
Sponsored Link

Java Buzz Forum
Format text using printf() method in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Format text using printf() method in java Posted: Mar 18, 2017 1:25 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Format text using printf() method in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
printf method in java:

  • Print() and println() methods are used to print text and object values or format data.
  • In order to format text we also have printf() method in java
  • Formatting data means displaying corresponding data type value. 
  • For example when print float ot double value if we want to specify upto 2 decimal numbers we user.
  • System.out.printf("%.2f", variable); 
  • Also alignment of string data. when we are printing string data in java using print() and pintf() 



#1. Write a program to print string using print() and printf() methods


  1. package printfinjava;
  2. /**
  3.  * How to format text using java printf() method
  4.  * @author www.instanceofjava.com
  5.  */
  6.  
  7. public class PrintfMethod {
  8.  
  9.     public static void main(String[] args) {
  10.         String str="java printf double";
  11.         
  12.         System.out.println ("String is "+str);
  13.         System.out.printf ("String is %s", str);
  14.  
  15.     }
  16.  
  17. }
Output:

  1. String is java printf double
  2. String is java printf double

java printf table



printf in java double int string


Format double using printf():
  • We can format double using printf() method in java
  • format double to 2 decimal places in java possible by using system.out.printf() method.

#2. Write a program to print double to 2 decimal places in java

  1. package printfinjava;
  2. /**
  3.  * How to format text using java printf() method
  4.  * @author www.instanceofjava.com
  5.  */
  6.  
  7. public class PrintfMethod {
  8.  
  9. public static void main(String[] args) {
  10.  
  11.      double value=12.239344;
  12.      System.out.printf("%.2f", value);
  13.  
  14. }
  15.  
  16. }
Output:

  1. 12.24

#3. Write a program to format text using printf() method in java


printf in java double

Read: Format text using printf() method in java

Topic: Becoming an Agile Leader, Part 2: Who to Approach Previous Topic   Next Topic Topic: 31% off Luma 3-Piece Whole Home Mesh WiFi System - Deal Alert

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use