如何使用 java.text.NumberFormat 来添加单位?
可能的重复:
如何将 1200 格式化为 1.2k java
当显示数字时,我想自动调整单位以限制数字的大小。例如,如果使用米和 22000 这样的数字,那么我想显示 22 km,但如果数字是 22,那么我只会显示 22 m。 似乎可以扩展 java.text.NumberFormat 来添加单位。如果是这样,你能澄清一下api是如何使用的吗(我已经 RTFM)。如果没有,您是否有其他建议的java api?
进一步的背景。我在 JFreeChart 中工作,并从其 API
Possible Duplicate:
How to go about formatting 1200 to 1.2k in java
When displaying a number I would like to automatically adjust the units to limit the size of numbers. For instance if working with meters and a number like 22000 then I would like to display 22 km, but if the number is 22 then I would just display 22 m.
It seems like java.text.NumberFormat
could be extended to add units. If so, can you clarify how the api is used (I've RTFM). If not, is there another java api you would suggest?
Further context. I'm working within JFreeChart and was led to NumberFormat from its API
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将所需的文字添加到
DecimalFormat
自定义TickUnits
。 < code>createStandardTickUnits() 方法是一个值得遵循的合理模型。另请参阅此答案。You can add the desired literals to the
DecimalFormat
instances used by your customTickUnits
. ThecreateStandardTickUnits()
method is a reasonable model to follow. See also this answer.