Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

java printf

作者:Cold—blooded凉薄   发布日期:2026-06-17   浏览:101

// Java 中没有直接的 printf 函数,但可以使用 System.out.printf 或者 String.format 来实现类似的功能。

// 示例 1: 使用 System.out.printf 输出格式化字符串
public class Main {
    public static void main(String[] args) {
        int number = 10;
        double pi = 3.14159;
        System.out.printf("The number is %d and the value of pi is %.2f", number, pi);
        // 输出: The number is 10 and the value of pi is 3.14
    }
}

// 示例 2: 使用 String.format 返回格式化后的字符串
public class Main {
    public static void main(String[] args) {
        int number = 10;
        double pi = 3.14159;
        String formattedString = String.format("The number is %d and the value of pi is %.2f", number, pi);
        System.out.println(formattedString);
        // 输出: The number is 10 and the value of pi is 3.14
    }
}

在上述代码中:

  • %d 用于表示整数。
  • %.2f 表示保留两位小数的浮点数。

上一篇:string在java中代表什么

下一篇:java定义一个数组

大家都在看

java url decode

java判断是windows还是linux

java原始数据类型

java连接数据库的代码

java date类型比较大小

java djl

ubuntu 卸载java

es java api

java常用的设计模式有哪些

java list 查找

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站