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/f5EW 著作权归作者所有。请勿转载和采集!