[英]Converting Decimal (Double) Value < 0.001 to String in Java
I have written a small method which is utilised for most of the Object to String conversions. 我写了一个小的方法,用于大多数对象到字符串的转换。
public String str(Object object) {
return object == null ? "" : object.toString();
}
When passing double d = 0.0003
to this method, I get an output of 3.0E-4
. 当将double d = 0.0003
传递给此方法时,我得到3.0E-4
的输出。 So I have altered the method like this. 所以我改变了这种方法。
public String str(Object object) {
return object == null ? "" : object instanceof Double && (double) object < 0.001 ? String.format("%.10f", object)
: object.toString();
}
But I kinda feel bad to check instanceof
for every conversions which is an additional check for non double objects. 但是我有点为每次转换检查instanceof
而感到难过,这是对非double对象的额外检查。 Is this the only way that I can convert double to exact string value or is it possible to convert correctly without an additional instance of check. 这是我可以将double转换为确切的字符串值的唯一方法,还是可以在没有附加检查实例的情况下正确转换。 Since if the object satisfies instanceof Double
, casting is of free cost and so I'm not worried about performance on casting. 由于如果对象满足instanceof Double
,则转换是免费的,因此我不必担心转换的性能。
I tried these steps, all produces E
output except String.format
我尝试了这些步骤,除了String.format
之外,所有均产生E
输出
double d = 0.0003; // 3.0E-4
System.out.println(String.valueOf(d)); // 3.0E-4
System.out.println(Double.toString(d)); // 3.0E-4
System.out.println(String.format("%.10f", d)); // 0.0003000000
you can provide different functions, for different types, like: 您可以针对不同类型提供不同的功能,例如:
public static String str(final Object object) {
return object == null ? "" : object.toString();
}
public static String str(final Double d) {
return d == null ? "" : str(d.doubleValue());
}
public static String str(final double d) {
return d < 0.001 ? String.format("%.10f", d) : String.valueOf(d);
}
You can use String.valueOf()
: 您可以使用String.valueOf()
:
public static void main(String[] args) {
final List<String> doubleValues = new ArrayList<>();
double value = 0.0;
doubleValues.add("5.01");
doubleValues.add("0.002");
doubleValues.add("0.00024");
doubleValues.add("0.000022");
for (String elem : doubleValues) {
value += Double.valueOf(elem);
}
System.out.printf("%.10f", value); // Formatting in print
System.out.println();
System.out.println(String.format("%.10f", value)); // Print with String formatting
double v = Double.parseDouble(String.valueOf(value)); //convert decimal number to double for print or calc
System.out.println(v);
}
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class PrintNumbers {
public static void main(String[] args) {
printNumberWOScientificNotations(0.1);
printNumberWOScientificNotations(0.001);
printNumberWOScientificNotations(0.00001);
System.out.println(0.00001);
}
static void printNumberWOScientificNotations(Object number) {
// Check if in scientific notation
if (String.valueOf(number).toLowerCase().contains("e")) {
System.out.println("Converting from e to number with 25 maximum fraction digits.");
NumberFormat formatter = new DecimalFormat();
formatter.setMaximumFractionDigits(25);
System.out.println(formatter.format(new Double(number.toString())));
} else {
System.out.println(String.valueOf(number));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.