Skip to content

Commit

Permalink
Make the decimal formatters culture invariant
Browse files Browse the repository at this point in the history
  • Loading branch information
toburger committed Nov 28, 2024
1 parent 35af20e commit 1ff1ec7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions convex-core/src/main/java/convex/core/text/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
Expand All @@ -17,6 +18,13 @@ public class Text {
private static final int WHITESPACE_LENGTH = 32;
private static final String ZEROS_9 = "000000000";
private static String WHITESPACE_32 = " "; // 32 spaces
private static DecimalFormatSymbols symbols;

static {
symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
}

/**
* Return true if the character is an ASCII numeric digit
Expand Down Expand Up @@ -65,18 +73,18 @@ public static String rightPad(long value, int length) {
return rightPad(Long.toString(value), length);
}

static DecimalFormat balanceFormatter = new DecimalFormat("#,###");
static DecimalFormat balanceFormatter = new DecimalFormat("#,###", symbols);

public static String toFriendlyNumber(long value) {
return balanceFormatter.format(value);
}

static DecimalFormat percentFormatter = new DecimalFormat("##.###%");
static DecimalFormat percentFormatter = new DecimalFormat("##.###%", symbols);
public static String toPercentString(double value) {
return percentFormatter.format(value);
}

static DecimalFormat decimalFormatter = new DecimalFormat("#,##0.####");
static DecimalFormat decimalFormatter = new DecimalFormat("#,##0.####", symbols);
public static String toFriendlyDecimal(double value) {
if (!Double.isFinite(value)) return CVMDouble.create(value).toString();
return decimalFormatter.format(value);
Expand Down

0 comments on commit 1ff1ec7

Please sign in to comment.