Formatting of Strings in Java

本文详细介绍了Java中字符串格式化的概念,包括使用printf命令进行字符串、整数和浮点数的格式化。例如,`%s`用于格式化字符串,`%d`用于格式化整数,`%f`用于格式化浮点数。通过指定宽度和对齐方式,可以控制输出的格式。示例代码展示了不同格式化选项的使用效果。
摘要由CSDN通过智能技术生成

Introduction

In Java, we can format strings depending on our needs and get the required output. The printf command is very useful in Java. The printf command takes text as a string and formats it depending on the instructions given to it.

 

String Formatting

  • "%s" Format a string with the required number of characters.
  • "%10s" Format a string with the specified number of characters and also right justify.
  • "%-10s" Format a string with the specified number of characters and also left justify.

For formatting numbers, we use the "d" character and for floating-point numbers we use the "f" character.

Example

  1. package demo;  
    public class Demo {  
     public static void main(String args[]) {  
      String n = "NAME";  
      System.out.printf("%10s %n", n);  
     }  
    }   

     

Output

 

string formatting right justify

 

Example

  1. package demo;  
    public class Demo {  
     public static void main(String args[]) {  
      String n = "NAME";  
      String l = "LOCATION";  
      System.out.printf("%-10s %10s %n", n, l);  
     }  
    }

Output

string formatting left justify

 

Integer Formatting

  • "%d" Format a string with the required numbers.
  • "%5d" Format a string with the required number of integers and also pad with spaces to the left side if integers are not adequate.
  • "%05d" Format a string with the required number of integers and also pad with zeroes to the left if integers are not adequate.

Example

  1. package demo;  
    public class Demo {  
     public static void main(String args[]) {  
      String r = "ROLL NO";  
      int i = 12345;  
      System.out.printf("%s %15d %n", r, i);  
     }  
    }  

     

Output

integer formatting

 

Example

  1. package demo;  
    public class Demo {  
     public static void main(String args[]) {  
      int r = 54321;  
      int i = 12345;  
      System.out.printf("%015d %15d %n", r, i);  
     }  
    }  

     

Output

 

integer formatting with zeroes

 

Floating Point Number Formatting

 

  • "%f" Format a string with the required numbers. It will always give six decimal places.
  • "%.3f" Format a string with the required numbers. It gives three decimal places.
  • "%12.3f" Format a string with the required numbers. The string occupies twelve characters. If numbers are not adequate then spaces are used on the left side of the numbers.

 

Example

  1. package demo;  
    public class Demo {  
     public static void main(String args[]) {  
      System.out.printf("%.3f %n", 123.45789);  
     }  
    }   

     

Output

 

floating point no. formatting

 

Example

  1. package demo;  
    public class Demo {  
     public static void main(String args[]) {  
      System.out.printf("%12.3f %n", 123.45789);  
     }  
    }  

     

Output

 

floating point no. formatting with spaces

 

 

Summary

This article explains string formatting in Java and the use of the printf command in Java.

 

reference:https://www.c-sharpcorner.com/UploadFile/3614a6/formatting-of-strings-in-java/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值