unitsofmeasurement / indriya

JSR 385 - Reference Implementation
Other
115 stars 40 forks source link

NumberDelimiterQuantityFormat ignores NumberFormat's maximum fraction digits #358

Closed max-maag closed 1 year ago

max-maag commented 2 years ago

Code:

final QuantityFormat format = new NumberDelimiterQuantityFormat.Builder()
    .setNumberFormat(new DecimalFormat("#.000"))
    .setUnitFormat(SimpleUnitFormat.getInstance())
    .build();

final Quantity<Length> quantity = Quantities.get(1.23456, METRES);

System.out.println(format.format(quantity));

Expected output: 1.235 m

Actual output: 1.23456 m

Elaboration: Since I am able to set a custom NumberFormat, I expect the NumberDelimiterQuantityFormat to use the NumberFormat unaltered.

Instead, NumberDelimiterQuantityFormat internally always sets the NumberFormat's maximum fraction digits so that all digits are displayed.

keilw commented 2 years ago

Thanks for pointing that out including a runnable snippet. We'll try to replicate and find the cause. @andi-huber Any thoughts about the precision influencing the NumberFormat?

bradh commented 2 years ago

I'm also hitting this issue (with a slightly different way to build the NumberFormat), and got pointed here after asking a StackOverflow question.

Is there a workaround that doesn't involve copy'n'paste of this code from Indriya?

keilw commented 1 year ago

Not sure, why this is open, it seems there's a PR (#364) addressing it?

bradh commented 1 year ago

The PR was closed (but not merged) because the barriers associated with the contributor agreement is too much work (needs employer lawyers involved - that has to be paid for / billed to someone).