0

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

Community
  • 1
  • 1
Adam
  • 3,675
  • 8
  • 45
  • 77
  • See also [`createStandardTickUnits()`](http://www.jfree.org/jfreechart/api/javadoc/src-html/org/jfree/chart/axis/NumberAxis.html#line.681) ff. – trashgod Aug 26 '11 at 18:35
  • @trashgod can you elaborate on the tick units paradigm. I understand how to use them to specify the frequency of ticks, but not how to control the labels. Thanks. – Adam Aug 26 '11 at 18:51
  • Thanks Suraj! Of the answers in the *dup of How to go about formatting* I'd like to point future folks to jzd's: http://stackoverflow.com/questions/4753251/how-to-go-about-formatting-1200-to-1-2k-in-java/4754692#4754692 – Adam Aug 26 '11 at 18:54

1 Answers1

1

You can add the desired literals to the DecimalFormat instances used by your custom TickUnits. The createStandardTickUnits() method is a reasonable model to follow. See also this answer.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045