How accurate is BigDecimal.doubleValue () if the operation does not apply to the sum

I NEED to send the value as " double ", but I:

  • Assuming the value long
  • Convert it to BigDecimal
  • The method to call scaleByPowerOfTen in BigDecimal (using "-2" to make cents an integer).
  • Call doubleValue () in BigDecimal to get the required value as double .

Now I know that double values ​​lose accuracy when applying arithmetic operations to them , but what can I get if I try to send a double as-is value based on BigDecimal, for example (I suppose) the above script.

This is important because I have to send monetary values ​​via SOAP this way - there is no other way.

I also have a script in which a BigDecimal is created using String , then doubleValue () is called on it . What can I get in this case?

EDIT: Sample code for this:

long amountInCents = 1852;
BigDecimal amountInWholeUnits = BigDecimal.valueOf(amountInCents).scaleByPowerOfTen(-2);
double amountToSend = amountInWholeUnits.doubleValue();

As for the case when the amount is provided as a string:

double amountToSend = new BigDecimal("18.52").doubleValue();

UPDATE

, , , , System.out.println: , , , SOAP. , , .

+4
3

1/100 , 1/100 0.01 , . $18.52 double double 18.52, 18.52.

+1

Java . , , double, .

BigDecimal , . , BigDecimal , double, . , BigDecimal.

, double. BigDecimal , . , , , , .

, (, ) .

0

OpenJDK BigDecimal, , double.

BigDecimal.valueOf(1852).scaleByPowerOfTen(-2);

BigDecimal () 1852 a scale of -2.

amountInWholeUnits.doubleValue();

, 0 ( ), :

Double.parseDouble(this.toString());

therefore, in essence, the results of the planned operations will be doubleas close to 18.52as possible with double.

Obviously, this also assumes that all JDKs are doing something similar (which may not be the case).

0
source

All Articles