【JAVA之String】字符串格式化

目前文章所包含的有以上内容

字符串格式化

首先是上官方API(源地址

Modifier and TypeMethod and Description
static Stringformat(Locale l, String format, Object… args)
Returns a formatted string using the specified locale, format string, and arguments.
static Stringformat(String format, Object… args)
Returns a formatted string using the specified format string and arguments.
format

引用官方API(跳转地址

public static String format(String format,Object… args)
Returns a formatted string using the specified format string and arguments.
The locale always used is the one returned by Locale.getDefault().

Parameters:
format - A format string
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns:
A formatted string
Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.

format

引用官方API(跳转地址

public static String format(Locale l,String format,Object… args)
Returns a formatted string using the specified locale, format string, and arguments.
Parameters:
l - The locale to apply during formatting. If l is null then no localization is applied.
format - A format string
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns:
A formatted string
Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification

简单说,2个格式化方法在于缺省Locale时,Locale就是Locale.getDefault(),String就是要格式化的模板,Object就是这个模板对应的各个值,如果这些对应不上的话,就会抛出IllegalFormatException (非法格式异常)

format之%f,%g

 System.out.println(String.format("%.2g",12.34));    //12
System.out.println(String.format("%.2g",123.34));    //1.2e+02
 System.out.println(String.format("%.5g",12.34));    //12.340
 System.out.println(String.format("%.2f",12.1234));  //12.12
 System.out.println(String.format("%.5f",12.1234));  //12.12340

结论:
%.ng代表着取所要格式化数字的位数,从左边第一位开始,不足补零,注意,这里如果整数位数比n还小的话,得到的String将会是科学计数法
%.nf代表着取所要格式化数字的小数点位数,从小数点后面开始,不足位数补零

附录(待补齐)

需求写法
百分号%%
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值