Round at one place for Localization.shortCount()

This commit is contained in:
bopol 2020-02-27 23:10:07 +01:00
parent ab4e1819c1
commit dfaa5675b6
2 changed files with 23 additions and 5 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}