Multiple decimal places for two in JAXB

I am creating XML with JAXB and I want to put double tags:

@XmlElement(name = "TaxFree") private double taxFreeValue; 

When I set the value using setTaxFreeValue(4.5); in tags, showing <TaxFree>4.5<TaxFree>

Is it possible in JAXB to get this <TaxFree>4.500<TaxFree> without wrapping double in a string?

+4
source share
2 answers

You can use the XmlAdapter to convert from a double value to the desired textual representation (String).

+2
source

The easiest way is

 double taxFreeValue; @XmlElement(name = "TaxFree") private String getTaxFree() { return String.format("%.3f", taxFreeValue); } 

Note that you can give this method any name and make it private to JAXB without worrying as soon as the annotation appears.

+1
source

Source: https://habr.com/ru/post/1479630/


All Articles