From dfaa5675b609ce9c2a2db75980d04ca573924f95 Mon Sep 17 00:00:00 2001 From: bopol Date: Thu, 27 Feb 2020 23:10:07 +0100 Subject: [PATCH] Round at one place for Localization.shortCount() --- .../org/schabi/newpipe/util/Localization.java | 17 ++++++++++++----- .../java/org/schabi/newpipe/util/Utils.java | 11 +++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/util/Utils.java diff --git a/app/src/main/java/org/schabi/newpipe/util/Localization.java b/app/src/main/java/org/schabi/newpipe/util/Localization.java index cd66ec3c2..ce06f14d2 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -26,6 +26,8 @@ import java.util.Date; import java.util.List; import java.util.Locale; +import static org.schabi.newpipe.util.Utils.round; + /* * Created by chschtsch on 12/29/15. * @@ -110,7 +112,7 @@ public class Localization { if (languageCode.length() == 2) { return new Locale(languageCode); } else if (languageCode.contains("_")) { - String country = languageCode.substring(languageCode.indexOf("_"), languageCode.length()); + String country = languageCode.substring(languageCode.indexOf("_")); return new Locale(languageCode.substring(0, 2), country); } } catch (Exception ignored) { @@ -120,6 +122,10 @@ public class Localization { } public static String localizeNumber(Context context, long number) { + return localizeNumber(context, (double) number); + } + + public static String localizeNumber(Context context, double number) { NumberFormat nf = NumberFormat.getInstance(getAppLocale(context)); return nf.format(number); } @@ -146,14 +152,15 @@ public class Localization { } public static String shortCount(Context context, long count) { + double value = (double) count; if (count >= 1000000000) { - return Long.toString(count / 1000000000) + context.getString(R.string.short_billion); + return localizeNumber(context, round(value / 1000000000, 1)) + context.getString(R.string.short_billion); } else if (count >= 1000000) { - return Long.toString(count / 1000000) + context.getString(R.string.short_million); + return localizeNumber(context, round(value / 1000000, 1)) + context.getString(R.string.short_million); } else if (count >= 1000) { - return Long.toString(count / 1000) + context.getString(R.string.short_thousand); + return localizeNumber(context, round(value / 1000, 1)) + context.getString(R.string.short_thousand); } else { - return Long.toString(count); + return localizeNumber(context, value); } } diff --git a/app/src/main/java/org/schabi/newpipe/util/Utils.java b/app/src/main/java/org/schabi/newpipe/util/Utils.java new file mode 100644 index 000000000..ee9a70b50 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/Utils.java @@ -0,0 +1,11 @@ +package org.schabi.newpipe.util; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class Utils { + + public static double round(double value, int places) { + return new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue(); + } +}