我想在没有指数形式的Java中打印double值.
double dexp = 12345678;
System.out.println("dexp: "+dexp);
Run Code Online (Sandbox Code Playgroud)
它显示了这个E符号:1.2345678E7.
我希望它像这样打印: 12345678
防止这种情况的最佳方法是什么?
我正在编写一个自定义序列化程序,将双值转换为JSON对象中的字符串.我的代码到目前为止:
public String toJson(Object obj) throws IOException {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("DoubleSerializer", new Version(1, 0, 0, ""));
module.addSerializer(Double.class, new DoubleSerializer());
mapper.registerModule(module);
return mapper.writeValueAsString(obj);
}
public class DoubleSerializer extends JsonSerializer<Double> {
@Override
public void serialize(Double value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
String realString = new BigDecimal(value).toPlainString();
jgen.writeString(realString);
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于Double(类成员),但不适用于double(基本类型)成员.例如,
public void test() throws IOException {
JsonMaker pr = new JsonMaker();
TestClass cl = new TestClass();
System.out.println(pr.toJson(cl));
}
class TestClass {
public …Run Code Online (Sandbox Code Playgroud)