Вы можете использовать jackson XmlMapper и написать и зарегистрировать DuoubleSerializer в формате, который соответствует вашим потребностям.
public class XmlTest {
public static class SimpleBean {
Double a;
Double b;
public Double getA() {
return a;
}
public void setA(Double a) {
this.a = a;
}
public Double getB() {
return b;
}
public void setB(Double b) {
this.b = b;
}
public SimpleBean(Double a, Double b) {
super();
this.a = a;
this.b = b;
}
}
public static class DoubleSerializer extends StdSerializer<Double> {
public DoubleSerializer() {
this(null);
}
public DoubleSerializer(Class<Double> t) {
super(t);
}
@Override
public void serialize(Double value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
gen.writeRaw(String.format(Locale.ROOT, "%.2f", value));
gen.writeEndObject();
}
}
public static void main(String[] args) throws JsonProcessingException {
XmlMapper xmlMapper = new XmlMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(Double.class, new DoubleSerializer());
xmlMapper.registerModule(module);
System.out.println(xmlMapper.writeValueAsString(new SimpleBean(1.50, 1.60)));
}
}