Java 数字格式化:将数字格式化为 MM
可以使用 Java 中的 DecimalFormat 类来格式化数字为'MM'。
示例代码:
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat('00');
int number = 9;
String formattedNumber = df.format(number);
System.out.println(formattedNumber); // Output: 09
}
}
在上面的示例代码中,我们创建了一个 DecimalFormat 对象,并使用'00' 格式化模式来格式化数字。然后使用 format() 方法将数字格式化为字符串。
注意:在格式化模式中,0 表示必须显示的数字位数,如果没有数字,则用 0 来填充。例如,如果格式化模式为'0000',则将 4 位数格式化为'0004',将 2 位数格式化为'0002',将 1 位数格式化为'0001'。
原文地址: https://www.cveoy.top/t/topic/otqd 著作权归作者所有。请勿转载和采集!