Skip to content
Snippets Groups Projects
Commit 515c9bd0 authored by Tim Molter's avatar Tim Molter
Browse files

number formatter - fixed for decimal numbers less than 1

parent 263b3075
No related branches found
No related tags found
No related merge requests found
......@@ -48,7 +48,7 @@ public class LineChart04 implements ExampleChart {
chart.getStyleManager().setLegendVisible(false);
for (int i = 0; i < 200; i++) {
Series series = chart.addSeries("A" + i, new double[] { Math.random(), Math.random() }, new double[] { Math.random(), Math.random() });
Series series = chart.addSeries("A" + i, new double[] { Math.random(), Math.random() }, new double[] { Math.random() / 1000, Math.random() / 1000 });
series.setLineColor(SeriesColor.BLUE);
series.setLineStyle(SeriesLineStyle.SOLID);
series.setMarker(SeriesMarker.CIRCLE);
......
......@@ -49,7 +49,13 @@ public class NumberFormatter {
else {
placeOfDifference = (int) Math.floor(Math.log(difference) / Math.log(10));
}
int placeOfValue = (int) Math.floor(Math.log(value) / Math.log(10));
int placeOfValue;
if (value == 0.0) {
placeOfValue = 0;
}
else {
placeOfValue = (int) Math.floor(Math.log(value) / Math.log(10));
}
System.out.println("difference: " + difference);
System.out.println("placeOfDifference: " + placeOfDifference);
......@@ -57,7 +63,7 @@ public class NumberFormatter {
if (placeOfDifference <= 4 && placeOfDifference >= -4) {
System.out.println("getNormalDecimalPattern");
return getNormalDecimalPattern(placeOfValue, placeOfDifference);
return getNormalDecimalPatternPositive(placeOfValue, placeOfDifference);
}
else {
System.out.println("getScientificDecimalPattern");
......@@ -65,13 +71,16 @@ public class NumberFormatter {
}
}
private String getNormalDecimalPattern(int placeOfValue, int placeOfDifference) {
private String getNormalDecimalPatternPositive(int placeOfValue, int placeOfDifference) {
int maxNumPlaces = 15;
StringBuilder sb = new StringBuilder();
for (int i = maxNumPlaces - 1; i >= -1 * maxNumPlaces; i--) {
if (i < placeOfValue && i >= placeOfDifference) {
if (i >= 0 && (i < placeOfValue && i >= placeOfDifference)) {
sb.append("0");
}
else if (i < 0 && (i > placeOfValue && i >= placeOfDifference)) {
sb.append("0");
}
else {
......
/**
* Copyright 2011 - 2014 Xeiam LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.Locale;
import org.junit.Test;
import com.xeiam.xchart.internal.chartpart.NumberFormatter;
/**
* @author timmolter
*/
public class NumberFormatterTest {
private final Locale locale = Locale.US;
@Test
public void testNumberFormatting() {
StyleManager styleManager = new StyleManager();
NumberFormatter numberFormatter = new NumberFormatter(styleManager);
// big
styleManager.setLocale(locale);
String stringValue = numberFormatter.formatNumber(1);
assertThat(stringValue, equalTo("1"));
stringValue = numberFormatter.formatNumber(1000);
assertThat(stringValue, equalTo("1,000"));
stringValue = numberFormatter.formatNumber(9999);
assertThat(stringValue, equalTo("9,999"));
stringValue = numberFormatter.formatNumber(20000);
assertThat(stringValue, equalTo("2E4"));
stringValue = numberFormatter.formatNumber(200.23);
assertThat(stringValue, equalTo("200.23"));
// small
stringValue = numberFormatter.formatNumber(0.01);
assertThat(stringValue, equalTo("0.01"));
stringValue = numberFormatter.formatNumber(0.001);
assertThat(stringValue, equalTo("0.001"));
stringValue = numberFormatter.formatNumber(0.0012);
assertThat(stringValue, equalTo("0.0012"));
stringValue = numberFormatter.formatNumber(0.0001);
assertThat(stringValue, equalTo("1E-4"));
stringValue = numberFormatter.formatNumber(.00012);
assertThat(stringValue, equalTo("1.2E-4"));
stringValue = numberFormatter.formatNumber(0.0);
assertThat(stringValue, equalTo("0"));
stringValue = numberFormatter.formatNumber(0);
assertThat(stringValue, equalTo("0"));
// non-default
styleManager.setLocale(Locale.GERMANY);
stringValue = numberFormatter.formatNumber(0.01);
assertThat(stringValue, equalTo("0,01"));
stringValue = numberFormatter.formatNumber(10000);
assertThat(stringValue, equalTo("10.000"));
stringValue = numberFormatter.formatNumber(200.23);
assertThat(stringValue, equalTo("200,23"));
styleManager.setDecimalPattern("#.#");
stringValue = numberFormatter.formatNumber(200.23);
assertThat(stringValue, equalTo("200,2"));
styleManager.setScientificDecimalPattern("0.#E0");
stringValue = numberFormatter.formatNumber(2009764.23);
assertThat(stringValue, equalTo("2E6"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment