Round at one place for Localization.shortCount()
This commit is contained in:
parent
ab4e1819c1
commit
dfaa5675b6
2 changed files with 23 additions and 5 deletions
|
@ -26,6 +26,8 @@ import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import static org.schabi.newpipe.util.Utils.round;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Created by chschtsch on 12/29/15.
|
* Created by chschtsch on 12/29/15.
|
||||||
*
|
*
|
||||||
|
@ -110,7 +112,7 @@ public class Localization {
|
||||||
if (languageCode.length() == 2) {
|
if (languageCode.length() == 2) {
|
||||||
return new Locale(languageCode);
|
return new Locale(languageCode);
|
||||||
} else if (languageCode.contains("_")) {
|
} 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);
|
return new Locale(languageCode.substring(0, 2), country);
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
|
@ -120,6 +122,10 @@ public class Localization {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String localizeNumber(Context context, long number) {
|
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));
|
NumberFormat nf = NumberFormat.getInstance(getAppLocale(context));
|
||||||
return nf.format(number);
|
return nf.format(number);
|
||||||
}
|
}
|
||||||
|
@ -146,14 +152,15 @@ public class Localization {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String shortCount(Context context, long count) {
|
public static String shortCount(Context context, long count) {
|
||||||
|
double value = (double) count;
|
||||||
if (count >= 1000000000) {
|
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) {
|
} 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) {
|
} 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 {
|
} else {
|
||||||
return Long.toString(count);
|
return localizeNumber(context, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
app/src/main/java/org/schabi/newpipe/util/Utils.java
Normal file
11
app/src/main/java/org/schabi/newpipe/util/Utils.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue