de.gsi.chart.utils.ScientificNotationStringConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of chartfx-chart Show documentation
Show all versions of chartfx-chart Show documentation
This charting library ${project.artifactId}- is an extension
in the spirit of Oracle's XYChart and performance/time-proven JDataViewer charting functionalities.
Emphasis was put on plotting performance for both large number of data points and real-time displays,
as well as scientific accuracies leading to error bar/surface plots, and other scientific plotting
features (parameter measurements, fitting, multiple axes, zoom, ...).
package de.gsi.chart.utils;
/*****************************************************************************
* *
* BI Common - convert Number <-> String *
* *
* modified: 2017-04-25 Harald Braeuning *
* *
****************************************************************************/
import java.text.DecimalFormat;
import javafx.util.StringConverter;
/**
* @author braeun
*/
public class ScientificNotationStringConverter extends StringConverter implements NumberFormatter {
private int precision = 2;
private final DecimalFormat format = new DecimalFormat();
public ScientificNotationStringConverter() {
buildFormat(precision);
}
public ScientificNotationStringConverter(final int precision) {
this.precision = precision;
buildFormat(precision);
}
@Override
public int getPrecision() {
return precision;
}
@Override
public NumberFormatter setPrecision(final int precision) {
this.precision = precision;
buildFormat(precision);
return this;
}
@Override
public boolean isExponentialForm() {
return true;
}
@Override
public NumberFormatter setExponentialForm(boolean state) {
return this;
}
@Override
public String toString(double val) {
return toString(Double.valueOf(val));
}
@Override
public String toString(final Number object) {
return format.format(object);
}
@Override
public Number fromString(final String string) {
return Double.parseDouble(string);
}
private void buildFormat(final int precision) {
final StringBuilder sb = new StringBuilder(32);
sb.append("0.");
for (int i = 0; i < precision; i++) {
sb.append('0');
}
sb.append("E0");
format.applyPattern(sb.toString());
}
}