04: Output floating-point numbers with 3 decimal places----java

Total time limit: 

1000ms memory limit: 65536kB

describe

Read in a single-precision floating-point number and output the floating-point number with 3 decimal places.

enter

There is only one line, a single precision floating point number.

output

There is only one line, the single-precision floating-point number read in.

sample input

12.34521

sample output

12.345
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        float n = s.nextFloat();
        //这里有两种输出方法
        /*为了便于在console中输出格式化的内容,java也提供一个printf   
           (String fmt, object...args); 方法,可用于简化println()方法中
            对格式化字符串输出的书写要求,比如以下两条语句在语法上是等价的:
        */
        System.out.println(String.format("%.3f",n));
        System.out.printf("%.3f",n);

    }
}

 

Guess you like

Origin blog.csdn.net/weixin_46279994/article/details/127323254