The cleanest way I've found is to use XMLAdapter
. Create a DoubleAdapter
class with:
public class DoubleAdapter extends XmlAdapter<String, Double> {
@Override
public Double unmarshal(String v) throws Exception {
if (v == null || v.isEmpty() || v.equals("null")) {
return null;
}
return Double.parseDouble(v);
}
@Override
public String marshal(Double v) throws Exception {
if (v == null) {
return null;
}
//Edit the format to your needs
return String.format("%.3f", v);
}
}
To use it, simply add the annotation.
@XmlElement(name = "TaxFree")
@XmlJavaTypeAdapter(DoubleAdapter.class)
private double taxFreeValue;