1

I'm using jfreechart to display axis values in milliseconds but I'd like to show the axis labels in seconds, so I'd use domainAxis.setNumberFormatOverride(new DecimalFormat("???"));

So what's the syntax to remove last three zeros? e.g.: 1.000, 2.000, 3.000 to 1, 2, 3.

julialecat
  • 169
  • 1
  • 1
  • 9
  • When the values are `1000, 2000, 3000` and you want to display instead `1, 2, 3` you need to divide them by `1000`. Seems the dot is the thausand separator in your locale. Correct me if my assumption is wrong. – SubOptimal Feb 24 '16 at 11:42
  • @SubOptimal Yep, I tried with different locales so I can use dot, coma or remove the thousand separator. The question is how to divide by `1000` using `DecimalFormat` syntax. – julialecat Feb 24 '16 at 12:01
  • You may be looking for `setDateFormatOverride()`, seen [here](http://stackoverflow.com/a/27696975/230513). – trashgod Feb 24 '16 at 12:34
  • Cross-posted [here](http://www.jfree.org/forum/viewtopic.php?f=3&t=117522). – trashgod Feb 25 '16 at 00:04

2 Answers2

1

Thanks to this other post I managed to divide by 1000 subclassing NumberFormat

So final code is simply:

private static final NumberFormat THOUSANDS = new NumberFormat() {

    @Override
    public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {

        new DecimalFormat().format(number / 1000D, toAppendTo, pos);

        return toAppendTo;
    }

    @Override
    public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
        return format((double) number, toAppendTo, pos);
    }

    @Override
    public Number parse(String source, ParsePosition parsePosition) {
        return null;
    }
};

And the call:

domainAxis.setNumberFormatOverride(THOUSANDS);

Community
  • 1
  • 1
julialecat
  • 169
  • 1
  • 1
  • 9
0

Try this

String label= "1.000"
String slabel= new DecimalFormat("0.####").format(Double.parseDouble(label));
System.out.println(slabel);
karthick23
  • 1,313
  • 1
  • 8
  • 15
  • Thanks @karthick23, it works using Strings but I don't know why it doesn't in jfreechart. Only the dots disappear :/ – julialecat Feb 24 '16 at 11:25