Decimalformat returns numeric instead zero 的格式化方法
Format method of Decimalformat returns numeric instead zero
我正在使用 DecimalFormat's
格式化方法来格式化带 6 个小数点的浮点值。所以我正在使用,
Float f = new Float("52.2815");
DecimalFormat decfo = new DecimalFormat("#0.000000");
String k = decfo.format(f);
我要打印 52.281500
。但它打印 52.281502
。如何获得52.281500
?
这是因为浮点数没有足够的位,因此您的输出数据变质了。您可以使用 Double。
import java.text.DecimalFormat;
public class Solution {
public static void main(String[] args) {
Double f = new Double("52.2815");
DecimalFormat decfo = new DecimalFormat("#.000000");
String k = decfo.format(f);
System.out.println(k);
}
}
输出:
52.281500
我正在使用 DecimalFormat's
格式化方法来格式化带 6 个小数点的浮点值。所以我正在使用,
Float f = new Float("52.2815");
DecimalFormat decfo = new DecimalFormat("#0.000000");
String k = decfo.format(f);
我要打印 52.281500
。但它打印 52.281502
。如何获得52.281500
?
这是因为浮点数没有足够的位,因此您的输出数据变质了。您可以使用 Double。
import java.text.DecimalFormat;
public class Solution {
public static void main(String[] args) {
Double f = new Double("52.2815");
DecimalFormat decfo = new DecimalFormat("#.000000");
String k = decfo.format(f);
System.out.println(k);
}
}
输出:
52.281500