From dfaa5675b609ce9c2a2db75980d04ca573924f95 Mon Sep 17 00:00:00 2001 From: bopol Date: Thu, 27 Feb 2020 23:10:07 +0100 Subject: [PATCH 1/2] 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(); + } +} From 22aa6d16a2115d2d3d9b688d7a6ccbc3ae9afc79 Mon Sep 17 00:00:00 2001 From: bopol Date: Fri, 28 Feb 2020 16:59:52 +0100 Subject: [PATCH 2/2] public Utils.round() moved to private Localization.round() --- .../java/org/schabi/newpipe/util/Localization.java | 7 ++++++- app/src/main/java/org/schabi/newpipe/util/Utils.java | 11 ----------- 2 files changed, 6 insertions(+), 12 deletions(-) delete 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 ce06f14d2..47b914bde 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -18,6 +18,8 @@ import org.ocpsoft.prettytime.units.Decade; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.localization.ContentCountry; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.text.DateFormat; import java.text.NumberFormat; import java.util.Arrays; @@ -26,7 +28,6 @@ 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. @@ -261,4 +262,8 @@ public class Localization { public static void assureCorrectAppLanguage(Context c) { changeAppLanguage(getAppLocale(c), c.getResources()); } + + private static double round(double value, int places) { + return new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue(); + } } diff --git a/app/src/main/java/org/schabi/newpipe/util/Utils.java b/app/src/main/java/org/schabi/newpipe/util/Utils.java deleted file mode 100644 index ee9a70b50..000000000 --- a/app/src/main/java/org/schabi/newpipe/util/Utils.java +++ /dev/null @@ -1,11 +0,0 @@ -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(); - } -}