Skip to content

Commit

Permalink
- Added locale support to number shortening with metric suffixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarathul committed Sep 23, 2017
1 parent 107b0e6 commit dd2aecd
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/main/java/net/zarathul/simplefluidtanks/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.zarathul.simplefluidtanks.tileentities.ValveBlockEntity;

import java.util.ArrayList;
import java.util.Locale;

/**
* General utility class.
Expand Down Expand Up @@ -191,7 +192,7 @@ public static int getComparatorLevel(float numerator, float denominator)
private static final char[] METRIC_SUFFIXES = { 'k', 'M', 'G', 'T', 'P', 'E' };

/**
* Shortens a number using metric suffixes and applies the provided format.
* Shortens a number using metric suffixes and applies the provided format and locale.
*
* @param number
* The number to shorten. Numbers lower than 1000 remain unchanged.
Expand All @@ -202,19 +203,44 @@ public static int getComparatorLevel(float numerator, float denominator)
* @param longFormat
* The string format to apply in case {@code number} is not shortened (2 arguments). The first argument is the
* unmodified number (decimal) and the second is the passed in object ({@code misc}) which can be anything.
* @param locale
* The locale to use for the number format.
* @param misc
* Can be used to add additional text to the output string.
* @return
* <c>null</c> if either {@code shortFormat} or {@code longFormat} is <c>null</c>, otherwise the potentially
* shortened and formatted number.
* <c>null</c> if either {@code shortFormat}, {@code longFormat} or {@code locale} is <c>null</c>, otherwise the
* potentially shortened and formatted number.
*/
public static String getMetricFormattedNumber(long number, String shortFormat, String longFormat, Object misc)
public static String getMetricFormattedNumber(long number, String shortFormat, String longFormat, Locale locale, Object misc)
{
if (shortFormat == null || longFormat == null) return null;
if (number < FACTOR) return String.format(longFormat, number, misc);
if (shortFormat == null || longFormat == null || locale == null) return null;
if (number < FACTOR) return String.format(locale, longFormat, number, misc);

int exponent = (int)(Math.log(number) / FACTOR_LOG);

return String.format(shortFormat, number / Math.pow(FACTOR, exponent), METRIC_SUFFIXES[exponent - 1], misc);
return String.format(locale, shortFormat, number / Math.pow(FACTOR, exponent), METRIC_SUFFIXES[exponent - 1], misc);
}

/**
* Shortens a number using metric suffixes and applies the provided format (UK locale).
*
* @param number
* The number to shorten. Numbers lower than 1000 remain unchanged.
* @param shortFormat
* The string format to apply in case {@code number} gets shortened (3 arguments). The first argument is the
* shortened number (floating point), the second is the metric suffix and the third is the passed in object
* ({@code misc}) which can be anything.
* @param longFormat
* The string format to apply in case {@code number} is not shortened (2 arguments). The first argument is the
* unmodified number (decimal) and the second is the passed in object ({@code misc}) which can be anything.
* @param misc
* Can be used to add additional text to the output string.
* @return
* <c>null</c> if either {@code shortFormat}, {@code longFormat} or {@code locale} is <c>null</c>, otherwise the
* potentially shortened and formatted number.
*/
public static String getMetricFormattedNumber(long number, String shortFormat, String longFormat, Object misc)
{
return getMetricFormattedNumber(number, shortFormat, longFormat, Locale.UK, misc);
}
}

0 comments on commit dd2aecd

Please sign in to comment.