当前位置: 首页 > news >正文

String.format() 用法详解

**String.format()详解示例:**

import java.util.Date;

/**

  • String.format()

  • 格式化
    /
    public class format {
    /
    *

    • 字符串
    • 占位符类型
    • %s 字符串类型
    • %c 字符类型
    • %b 布尔类型
    • %d 整数类型(十进制)
    • %x 整数类型(十六进制)
    • %o 整数类型(八进制)
    • %f 浮点类型
    • %a 十六进制浮点类型
    • %e 指数类型
    • %g 通用浮点类型
    • %h 散列码
    • %d% 百分比类型
    • %n 换行符
    • %tx 日期与时间类型

    */
    public static void main(String[] args) {
    String str;
    // %s
    str = String.format(“Hi,%s”, “布鲁斯”);
    System.out.println(“%s格式化之后=========”+str);
    // %c %n
    str = String.format(“字母c的大写是:%c %n”, ‘C’);
    System.out.println(“%c格式化==”+str);
    // %b
    str = String.format(“布尔结果是:%b”, 3>2);
    System.out.println(“%b格式化===”+str);
    //%d
    str = String.format(“100的一半是%d”, 100/2);
    System.out.println(“%d格式化===”+str);
    // %x
    str = String.format(“100的16进制数是:%x”, 100);
    System.out.println(“%x格式化===”+str);
    // %o
    str = String.format(“100的8进制数是:%o”, 100);
    System.out.println(“%o格式化===”+str);
    // %f
    str = String.format(“50元的书打8.5折扣是:%f 元”, 50 * 0.85);
    System.out.println(“%f格式化===”+str);
    // %a
    str = String.format(“上面价格的16进制数是:%a”, 50 * 0.85);
    System.out.println(“%a格式化===”+str);
    // %e
    str = String.format(“上面价格的指数表示:%e”, 50 * 0.85);
    System.out.println(“%e格式化===”+str);
    // %g
    str = String.format(“上面价格的指数和浮点数结果的长度较短的是:%g”, 50 * 0.85);
    System.out.println(“%g格式化===”+str);
    // %d%
    str = String.format(“上面的折扣是:%d%%”, 85);
    System.out.println(“%d%格式化===”+str);
    // %h
    str = String.format(“字母A的散列码是:%h”,‘A’);
    System.out.println(“%h格式化===”+str);

     /**** +  为正数或负数添加符号* 0  在数字位数不够的地方补上0* 空格 在位数不够的地方补上空格* ,  对数字分组,三位一隔**/// +str = String.format("数字的正负表示:%+d %d %+d %d",8,8,-8,-8);System.out.println(str);// -str = String.format("左对齐:%-6d",8);System.out.println(str);// 0str = String.format("缺位补零:%06d",8);System.out.println(str);// 空格str = String.format("缺位补空格:% 6d",8);System.out.println(str);str = String.format("缺位补空格:% 6d",-8);System.out.println(str);// ,str = String.format("数字分组:%,d",123456789);System.out.println(str);// (str = String.format("括号用法:%(d",-8888);System.out.println(str);str = String.format("括号用法:%(d",8888);System.out.println(str);// #str = String.format("#括号用法(十六进制):%#x",12);System.out.println(str);str = String.format("#括号用法(八进制):%#o",12);System.out.println(str);// <str = String.format("<括号用法:%f %<3.1f",3.14,3.2);// "%<3.1f"作用的对象是前一个"%f"所作用的对象System.out.println(str);/****  C  包括全部日期和时间信息*  F  "年-月-日" 格式*  D  '月/日/年' 格式*  r  'HH:MM:SS PM' 格式(12小时制)*  T  'HH:MM:SS' 格式(24小时制)*  R  'HH:MM' 格式(24小时制)**/// cstr = String.format("全部日期和时间信息:%tc",new Date());System.out.println(str);// Fstr = String.format("年-月-日格式:%tF",new Date());System.out.println(str);//Dstr = String.format("月/日/年格式:%tD",new Date());System.out.println(str);//rstr = String.format("HH:MM:SS PM格式(12时制):%tr",new Date());System.out.println(str);//Tstr = String.format("HH:MM:SS格式(24时制):%tT",new Date());System.out.println(str);//Rstr = String.format("HH:MM格式(24时制):%tR",new Date());System.out.println(str);// %.2f保留两位小数str = String.format("3.14保留两位小数:%.2f",3.141);System.out.println(str);
    

    }

}

http://www.lryc.cn/news/472758.html

相关文章:

  • es 常用命令(已亲测)
  • RabbitMQ 高级特性——事务
  • HCIP-HarmonyOS Application Developer V1.0 笔记(二)
  • 初体验鸿蒙 HarmonyOS NEXT开发
  • MySQL---主从复制和读写分离
  • Apache Kyuubi概述——网易数帆(网易杭州研究院)开源
  • 前端代码注释
  • Linux线程安全(二)条件变量实现线程同步
  • Linux初阶——线程(Part2):互斥同步问题
  • 力扣——二叉树的后序遍历(C语言)
  • 利用kimi编程助手从0到1开始搭建小程序!
  • WSL(Ubuntu20.04)编译和安装DPDK
  • HLS协议之nginx-hls-多码率测试环境搭建
  • 函数式接口与回调函数实践
  • Windows11系统如何使用自带的录音、录屏工具?
  • 使用 web (vue 和DRF))实现 模拟一个IDE 功能思路
  • 智航船舶租赁综合管理系统
  • 统信UOS下启动图形界面应用工具monitor报JAVA相关错:An error has occurred. See the log file
  • N-154基于springboot酒店预订管理系统
  • 微信小程序如何实现地图轨迹回放?
  • vscode的一些使用心得
  • Python金色流星雨(完整代码)
  • [山河CTF 2024] week3
  • Java集合常见面试题总结(5)
  • 牛客网刷题(3)(Java的几种常用包)
  • PyTorch nn.Conv2d 空洞卷积
  • 像素、分辨率、PPI(像素密度)、帧率的概念
  • 两步GMM计算权重矩阵
  • leetcode452. 用最少数量的箭引爆气球
  • 【Android】使用TextView实现按钮开关代替Switch开关