Home  >  Article  >  Java  >  What are the ways to format output in Java?

What are the ways to format output in Java?

PHPz
PHPzforward
2023-05-08 13:40:074079browse

    Java console output

    1. Use System.out.write method output

    You can output information to the console The write method using the output stream is just not as convenient to use as print and println. In fact, the print and println methods also encapsulate write and ultimately call write to write data to the console.
    When using write, you need to import IOException, that is, add import java.io.IOException in front of the java file; the write method may throw IOExcetion.
    I won’t introduce the write method in detail. It is better to use print or println usually.

    2 Use the System.out.println method to output

    To view it in idea, enter System.out .println() actually calls the following methods (function overloading)

    What are the ways to format output in Java?

    After calling System.out.println(), a newline will be added directly after the output. If you do not write parameters, it will only serve as a line break.

    If the parameter is char[], all elements in char[] will be output directly without any separators between the elements, as if a string is output.

    Another special parameter is Object. Object is the parent class of all classes in Java. If the parameter is an Object object, the address of the Object or null will be output directly unless the corresponding Object is overridden. I have my own toString method, so I won’t go into too much detail here.

    We can use string concatenation operations to output various formats, such as output: x=5, output the value of x

    int x=5;
    System.out.println("x="+x);

    3 Using System The .out.print method output

    is only one less method without parameters than System.out.println().

    The only difference between System.out.print and System.out.println is that this one does not add newlines.

    What are the ways to format output in Java?

    4 System.out.printf

    The bottom layer of printf is format. There is no difference in the usage of the two methods. In fact, printf just calls Just format it

    What are the ways to format output in Java?

    Detailed introduction to formatted output

    System.out.format(); System.out.printf();

    The usage of both is the same. The format is used as an example below.

    When we need to format and output some data, just using System.out.println(); is not enough. At this time, we need to use the format method

    System .out provides the formatted output method format. The first parameter of format is a template. The template has some placeholders. The following parameters are used to replace the placeholders in the template to complete the output of the entire information.

    For example:

    String name="LiLi";
    int age=8;
    char sex='男';
    float grade=52.5;
    System.out.format("姓名:%s, 年龄:%d, 性别:%c, 成绩:%.1f\n", name, age, sex, grade);

    Output: Name: LiLi, Age: 8, Gender: Male, Score: 52.5

    %c, %s, etc. are called placeholders symbols, representing characters and strings respectively, which will be replaced by the values ​​of the corresponding variables that appear later. Like %.1f, .f is added to %f to indicate outputting a 1-digit decimal.

    Commonly used placeholders:

    Placeholder Description
    %d Formatted output integer
    %f Formatted output floating point number
    %e Format floating point number in scientific notation
    %s Format output string

    当然不可能只有这么点了,下边才是大头????

    格式化整数

    既可以格式化基本数据类型,也可以格式化它们的包装类

    • %d :格式化十进制整数

    • %o :格式化八进制整数

    • %x :格式化小写十六进制整数,如:abc58

    • %X :格式化大写十六进制整数,如:ABC58

    修饰符
    • “+”:格式化正整数时强制添加上正好,如%+d可将123格式化位+123

    • “,”:格式化整数时按“千”分组,例如:%,d可将1234567890格式化位1,234,567,890

    它俩可以组合使用,如:%+,d

    数据的宽度

    %md或%-md指定格式化整数的长度最少为m

    如%6d可将123格式化为“ 123”,左边三个空格,即如果数字的长度不足6,则在格式化的数字左边添加空格使得整体长度为6(左边添加空格即右对齐

    %-6d使得结果左对齐,即在右边添加空格,如%-6d可将123格式化为“123 ”,右边三个空格

    格式化浮点数

    既可以格式化基本数据类型,也可以格式化它们的包装类

    修饰符
    • “+” :格式化正数时添加正号

    • “,”:将正数部分按千分位

    它俩可以组合使用,如:%+,f

    限制小数位数与宽度
    • “%.nf”可以限制小数的位数为n(遵循四舍五入)

    • “%mf”可以规定数据的宽度为m,长度不足补空格,默认居右(%-mf,居左)

    组合使用:

    System.out.printf("%7.3f",0.1235);

    结果:0.124 (还有俩个空格,“.”也占一个长度)

    String也可以调用format方法

    String也可以调用format方法,用法与上述完全一致,结果返回一个字符串对象

    例:

    String s=String.format("%d",12345);

    s的值为12345

    The above is the detailed content of What are the ways to format output in Java?. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete