Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Several decimal for double in JAXB

I create XML with JAXB, and I want to put double inside tags:

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

When I set value with setTaxFreeValue(4.5); in tags shows <TaxFree>4.5<TaxFree>

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

like image 568
Juraj Ćutić Avatar asked May 07 '13 22:05

Juraj Ćutić


People also ask

How many decimal points can double have?

double is a 64-bit IEEE 754 double precision Floating Point Number – 1 bit for the sign, 11 bits for the exponent, and 52* bits for the value. double has 15 decimal digits of precision.

How do you double up two decimal places?

Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places.

How many decimals can a double hold Java?

This is possible to do because a float value can hold only a maximum of 7 digits after the decimal, while a double value in Java can hold a maximum of 16 digits after the decimal.


Video Answer


1 Answers

The simplest way is this

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 JAXB dont care as soon as the annotation is present.

like image 199
Evgeniy Dorofeev Avatar answered Oct 06 '22 00:10

Evgeniy Dorofeev