在 double 前面加零
Adding zeros in front of double
我有双人
例如:-2.34
我想在前面加9个零,让长度固定为10位
结果:-0000000002.34
我试过了:
String formatted = String.format("%010d", number);
但是我们可以将 double 传递给字符串格式化函数吗?
最有效的方法是什么?
最简单的方法是创建一个 DecimalFormat
object,自己提供模式。 0
字符表示应该在此处打印一个数字,即使通常不需要。
DecimalFormat df = new DecimalFormat("0000000000.00");
String formatted = df.format(-2.34);
输出:
-0000000002.34
使用 String.format
,您可以提供总长度以及使用的任何小数字符。使用14
作为总长度,占10位"whole"位,负号,小数点,2位小数。
String.format("%014.2f", test)
输出为:
-0000000002.34
我有双人
例如:-2.34
我想在前面加9个零,让长度固定为10位
结果:-0000000002.34
我试过了:
String formatted = String.format("%010d", number);
但是我们可以将 double 传递给字符串格式化函数吗?
最有效的方法是什么?
最简单的方法是创建一个 DecimalFormat
object,自己提供模式。 0
字符表示应该在此处打印一个数字,即使通常不需要。
DecimalFormat df = new DecimalFormat("0000000000.00");
String formatted = df.format(-2.34);
输出:
-0000000002.34
使用 String.format
,您可以提供总长度以及使用的任何小数字符。使用14
作为总长度,占10位"whole"位,负号,小数点,2位小数。
String.format("%014.2f", test)
输出为:
-0000000002.34