使用NumberFormat格式化数字时的java字段位置
有人能用通俗易懂的英语解释下面从这个API中摘取的内容吗
You can also use forms of the parse and format methods with ParsePosition and FieldPosition to allow you to:
progressively parse through pieces of a string
align the decimal point and other areas
For example, you can align numbers in two ways: If you are using a monospaced font with spacing for alignment, you can pass the FieldPosition in your format call, with field = INTEGER_FIELD. On output, getEndIndex will be set to the offset between the last character of the integer and the decimal. Add (desiredSpaceCount - getEndIndex) spaces at the front of the string.
我不知道FieldPosition有什么用,上面发布的API也没有帮助(至少对我是这样)。 一个简单的例子显示了输出将是超级! 提前谢谢
# 1 楼答案
要使用数字格式,首先必须获得一个区域设置实例
然后您可以设置格式化的许多属性。例如,您可以选择显示逗号、限制小数位数以及设置最小和最大整数长度。如果要相对于区域设置显示“%”,则必须使用NumberFormat。只是不要将“%”作为字符串附加到结果中。你想用(3745)代替“-”来表示一个负数,然后用NumberFormat。像这些,有许多用途
您可以检查JavaDoc以了解更多方法
这会告诉你怎么做
这就是如何使用数字格式的字段位置
# 2 楼答案
FieldPosition和ParsePosition类的java文档给出了更多提示
基本上,如果您不想格式化一个完整的日期或数字,而只想格式化其中的一部分(例如,如果您的UI将金额拆分为几个部分(例如在两个输出字段中提供美元和美分),则可以使用FieldPosition。如果您需要类似的东西,您可以使用FieldPosition来检索您感兴趣的零件。 对于ParsePosition,我现在没有一个好的用例,也许其他人可以在这里提供帮助