Skip to content

Commit

Permalink
Use DecomalFormat.setMaximumFractionDigits & keep negative zero
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas ADAM <tadam@silicom.fr>
  • Loading branch information
tadam50 committed May 17, 2022
1 parent 0eeed13 commit 251fbe8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.powsybl.sld.svg;

import org.apache.commons.math3.util.Precision;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
Expand Down Expand Up @@ -43,10 +41,11 @@ public DirectionalFeederInfo(String componentType, double value, int precision,
}

private static String format(double value, int precision) {
// build pattern
String pattern = precision == 0 ? "#;#" : "#.0" + "#".repeat(precision - 1);
String formattedValue = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(Locale.ENGLISH)).format(value);
return formattedValue.replaceAll("^-(?=0(\\.0*)?$)", ""); // Avoid negative zero
DecimalFormat format = new DecimalFormat();
format.setMaximumFractionDigits(precision);
// Floating representation with point
format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH));
return format.format(value);
}

public DiagramLabelProvider.LabelDirection getDirection() {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 251fbe8

Please sign in to comment.