[英]How to get full unit name in jscience
I'm using jscience to parse some units for me: 我正在使用jscience为我解析一些单位:
Unit<?> meter = Unit.valueOf("m");
System.out.println(meter);
Unit<?> foot = Unit.valueOf("ft");
System.out.println(foot);
This prints out : 打印输出:
m
ft
I'd like it to print out 我想打印出来
metre
foot
Or something like this. 或类似的东西。 In reality I'm actually reading in these unit strings, I have no control over what the user types, so I'd expect some to type "m", some to type "metre". 实际上,我实际上是在读取这些单位字符串,我无法控制用户键入的内容,因此我希望有人键入“ m”,有人键入“ metre”。 When displaying it back, I'd like to always display it back as "Metre". 当将其显示回来时,我想始终将其显示为“ Metre”。
[Limitation] [局限性]
The program is a large data storage program storing many different types of quantities. 该程序是一个大型数据存储程序,用于存储许多不同类型的数量。 The user can store any quantity of any dimension (as long as it's defined in UCUM). 用户可以存储任何数量的任何尺寸(只要在UCUM中定义)。
You can use the UnitFormat
class to specify how these classes get printed. 您可以使用UnitFormat
类来指定如何打印这些类。 Use the label
method to specify the name, and the alias
method to assist with parsing. 使用label
方法来指定名称,并使用alias
方法来帮助解析。
import java.io.IOException;
import javax.measure.unit.Unit;
import javax.measure.unit.UnitFormat;
public class ScienceUnit {
public static void main(String[] args) throws IOException {
Unit<?> meter = Unit.valueOf("m");
StringBuilder sb = new StringBuilder();
UnitFormat instance = UnitFormat.getInstance();
instance.label(meter, "Metre");
instance.format(meter, sb);
System.out.println(sb.toString());
}
}
Output: 输出:
Metre
Note that if you remove the instance.label
line from this program, the output is: 请注意,如果您从该程序中删除instance.label
行,则输出为:
m
The default value is the short form that you describe in your original post. 默认值为您在原始帖子中描述的简短格式。 I assume that the default values are the short form values described by UCUM , but I haven't actually checked. 我假设默认值是UCUM描述的简短格式值 ,但实际上并没有检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.