Skip to content

Commit

Permalink
Merge pull request #516 from toburger/fix/invariant-culture
Browse files Browse the repository at this point in the history
Make the decimal formatters culture invariant
  • Loading branch information
mikera authored Nov 29, 2024
2 parents 89c82a1 + 1ff1ec7 commit 502e7c3
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 502e7c3

Please sign in to comment.