Removing last three zeros with DecimalFormat

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
julialecat
Posts: 3
Joined: Wed Feb 24, 2016 11:20 am
antibot: No, of course not.

Removing last three zeros with DecimalFormat

Post by julialecat » Wed Feb 24, 2016 12:49 pm

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

Code: Select all

domainAxis.setNumberFormatOverride(new DecimalFormat("0.####"));
But the code above only removes the dots from 1.000, 2.000, 3.000, etc.

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

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Removing last three zeros with DecimalFormat

Post by david.gilbert » Wed Feb 24, 2016 12:54 pm

Code: Select all

new DecimalFormat("0")
But that will look odd if the axis ever has a tick unit that is less that one second (for example, half a second). That won't be an issue if you've specified the tick unit manually, only if you leave it to be auto-selected by JFreeChart.

If you want auto-tick unit selection (which is better if the chart can be resized or the data range changes significantly) then the following will ensure that only integer values are used:

Code: Select all

xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

julialecat
Posts: 3
Joined: Wed Feb 24, 2016 11:20 am
antibot: No, of course not.

Re: Removing last three zeros with DecimalFormat

Post by julialecat » Wed Feb 24, 2016 1:11 pm

Hi David,

Well, maybe it wasn't clear that the dots were thousand separator in my locale so it doesn't seem to work. Any other option?

Don't worry about tick units less than one second. I'm using:

Code: Select all

domainAxis.setTickUnit(new NumberTickUnit(1000));

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Removing last three zeros with DecimalFormat

Post by david.gilbert » Wed Feb 24, 2016 1:25 pm

Probably a good approach would be to subclass NumberFormat (or decimal format) and divide incoming numbers by 1000 before formatting them.

Here's an example that formats in millions (whereas I think you want thousands)

http://stackoverflow.com/a/529476/2592874
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

julialecat
Posts: 3
Joined: Wed Feb 24, 2016 11:20 am
antibot: No, of course not.

Re: Removing last three zeros with DecimalFormat

Post by julialecat » Wed Feb 24, 2016 1:41 pm

Great, thank you so much for the tip.

Final code is:

Code: Select all

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;
		}
	};

Code: Select all

domainAxis.setNumberFormatOverride(THOUSANDS);

Locked