简体   繁体   English

JSR 363:以分贝格式格式化卷单位

[英]JSR 363 : formating a volume unit in decilitre

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"? 如何格式化卷单位“ DECILITRE”以显示“ dl”?

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 . https://github.com/unitsofmeasurement/unit-ri/issues/54显示,前缀不会在SimpleUnitFormat所有单元之间自动传播。

For out-of-the-box units coming with an implementation, SimpleUnitFormat should know them. 对于实现附带的现成的单元,SimpleUnitFormat应该知道它们。 Will check out, if it can be addressed, see https://github.com/unitsofmeasurement/unit-ri/issues/60 . 将检查出是否可以解决,请参阅https://github.com/unitsofmeasurement/unit-ri/issues/60

Most other UnitFormat implementations eg those found in the Java SE 8+ implementation uom-se or extension modules deal with this in a different way. 大多数其他UnitFormat实现,例如在Java SE 8+实现uom-se或扩展模块中找到的实现,都以不同的方式处理。 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. 这是SimpleUnitFormat的已知局限性(这就是为什么将其称为“简单”),以确保其在运行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. 如果您的应用程序能够使用Java SE 8和更高版本,或者更新Java版本没有问题,请考虑这一点。 At this point the RI must be backward compatible with ME Embedded, so the default UnitFormat is SimpleUnitFormat for all implementations. 此时,RI必须与ME Embedded向后兼容,因此所有实现的默认UnitFormatSimpleUnitFormat 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. 在Java SE实现的基础上,您将获得EBNFUnitFormat,并且基于单元测试和缺少问题报告,它应该可以在此处运行。

Regards, Werner 问候,沃纳

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM