Android 将cool格式转换为两个小数点

Android 将cool格式转换为两个小数点,android,numbers,format,Android,Numbers,Format,我使用下面的方法来转换格式的数字,如 private static String coolFormat(double n, int iteration) { double d = ((long) n / 100) / 10.0; boolean isRound = (d * 10) %10 == 0;//true if the decimal part is equal to 0 (then it's trimmed anyway) return (d < 100

我使用下面的方法来转换格式的数字,如

 private static String coolFormat(double n, int iteration) {
    double d = ((long) n / 100) / 10.0;
    boolean isRound = (d * 10) %10 == 0;//true if the decimal part is equal to 0 (then it's trimmed anyway)
    return (d < 1000? //this determines the class, i.e. 'k', 'm' etc
            ((d > 99.9 || isRound || (!isRound && d > 9.99)? //this decides whether to trim the decimals
                    (int) d * 10 / 10 : d + "" // (int) d * 10 / 10 drops the decimal
            ) + "" + c[iteration])
            : coolFormat(d, iteration+1));

}

如果我将输入1520 OUTPUT=>1.5k,但OUTPUT应该是1.52k

请尝试以下方法,它应该适合您

public static String formatNumberExample(Number number) {
    char[] suffix = {' ', 'k', 'M', 'B', 'T', 'P', 'E'};
    long numValue = number.longValue();
    int value = (int) Math.floor(Math.log10(numValue));
    int base = value / 3;
    if (value >= 3 && base < suffix.length) {
        return new DecimalFormat("#0.00").format(numValue / Math.pow(10, base * 3)) + suffix[base];
    } else {
        return new DecimalFormat("#,##0").format(numValue);
    }
}
public静态字符串格式numberexample(Number){
char[]后缀={'','k','M','B','T','P','E'};
long numValue=number.longValue();
int值=(int)Math.floor(Math.log10(numValue));
int base=值/3;
如果(值>=3&&base
证明:

从您的代码中完全不清楚您想做什么。举例说明你所拥有的和你想要的result@VladyslavMatviienko我已经更新了问题。我需要小数点后两位的结果。目前它只给出一个小数点。什么是迭代?代码太乱了,至少有一部分是不可能理解的。原始线程@SamosysTechnologies投票赞成并接受答案,如果它真的节省了您的时间。我没有足够的声誉投票,但从我的角度来看,它是投票赞成的答案。@SamosysTechnologies谢谢。!
public static String formatNumberExample(Number number) {
    char[] suffix = {' ', 'k', 'M', 'B', 'T', 'P', 'E'};
    long numValue = number.longValue();
    int value = (int) Math.floor(Math.log10(numValue));
    int base = value / 3;
    if (value >= 3 && base < suffix.length) {
        return new DecimalFormat("#0.00").format(numValue / Math.pow(10, base * 3)) + suffix[base];
    } else {
        return new DecimalFormat("#,##0").format(numValue);
    }
}