问题
Formatting a volume unit works correctly in millilitres and centilitres but fails for decilitres.
import static tec.units.ri.unit.Units.LITRE;
import javax.measure.Unit;
import javax.measure.format.UnitFormat;
import javax.measure.quantity.Volume;
import javax.measure.spi.ServiceProvider;
import static tec.units.ri.unit.MetricPrefix.*;
public class Example {
public static void main(String[] args) {
final UnitFormat unitFormat = ServiceProvider.current().getUnitFormatService().getUnitFormat();
final Unit<Volume> MILLILITRE = MILLI(LITRE);
final Unit<Volume> CENTILITRE = CENTI(LITRE);
final Unit<Volume> DECILITRE = DECI(LITRE);
final String mL = unitFormat.format(MILLILITRE);
final String cL = unitFormat.format(CENTILITRE);
final String dL = unitFormat.format(DECILITRE);
System.out.println(mL);
System.out.println(cL);
System.out.println(dL);
}
}
This code prints :
ml
cl
㎥/10000.0
How to format the volume unit "DECILITRE" to display "dl"?
回答1:
Thanks for bringing this to our attention.
https://github.com/unitsofmeasurement/unit-ri/issues/54
shows, that prefixes are not automatically propagated across all units in SimpleUnitFormat
.
For out-of-the-box units coming with an implementation, SimpleUnitFormat should know them. Will check out, if it can be addressed, see https://github.com/unitsofmeasurement/unit-ri/issues/60.
Most other UnitFormat
implementations e.g. those found in the Java SE 8+ implementation uom-se or extension modules deal with this in a different way. It is a known limitation of SimpleUnitFormat
(which is why it's called "simple") to ensure it works the same way on very small systems running Java ME Embedded. If your application is able to use Java SE 8 and above or you have no problem updating the Java version, please consider this. At this point the RI must be backward compatible with ME Embedded, so the default UnitFormat
is SimpleUnitFormat
for all implementations. Calling
final UnitFormat unitFormat =
ServiceProvider.current().getUnitFormatService().getUnitFormat("EBNF");
on top of the Java SE implementation will get you the EBNFUnitFormat and based on unit tests and lack of issue reports it should work there.
Regards, Werner
来源:https://stackoverflow.com/questions/43308544/jsr-363-formating-a-volume-unit-in-decilitre