Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将2k、1m、1g等转换为2000、1000000等_Java_Android - Fatal编程技术网

Java 如何将2k、1m、1g等转换为2000、1000000等

Java 如何将2k、1m、1g等转换为2000、1000000等,java,android,Java,Android,你好,我是java新手,有人能帮我吗 这段代码需要转换 2000至2k和1000000至1m等 private static final NavigableMap<Long, String> suffixes = new TreeMap<>(); static { suffixes.put(1_000L, "k"); suffixes.put(1_000_000L, "M"); suffixes.put(1_000

你好,我是java新手,有人能帮我吗

这段代码需要转换 2000至2k和1000000至1m等

 private static final NavigableMap<Long, String> suffixes = new TreeMap<>();
    static {
        suffixes.put(1_000L, "k");
        suffixes.put(1_000_000L, "M");
        suffixes.put(1_000_000_000L, "G");
        suffixes.put(1_000_000_000_000L, "T");
        suffixes.put(1_000_000_000_000_000L, "P");
        suffixes.put(1_000_000_000_000_000_000L, "E");
    }

    public String format(long value) {
        //Long.MIN_VALUE == -Long.MIN_VALUE so we need an adjustment here
        if (value == Long.MIN_VALUE) return format(Long.MIN_VALUE + 1);
        if (value < 0) return "-" + format(-value);
        if (value < 1000) return Long.toString(value); //deal with easy case

        Map.Entry<Long, String> e = suffixes.floorEntry(value);
        Long divideBy = e.getKey();
        String suffix = e.getValue();

        long truncated = value / (divideBy / 10); //the number part of the output times 10
        boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 10);
        return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix;
    }
我需要一个反向转换的代码,例如
2k到2000,1m到1000000,我已经发布了一个解决方案。需要适当的输入,因为不包括适当的验证。让我知道它是否适合你的需要。您还可以根据需要添加/修改开关箱

private static final String dp(String value) {
    if (value.contains("k") || value.contains("K")){
        value.replace("k", "000");
        value.replace("K", "000");
    } else if (value.contains("m") || value.contains("M")){
        value.replace("m", "000000");
        value.replace("M", "000000");
    } else if (value.contains("g") || value.contains("G")){
        value.replace("g", "000000000");
        value.replace("G", "000000000");
    } ......


    return value;
}
public class Test {
    public static void main(String[] args) {
        String values[] = {
                "27",
                "999",
                "1.0 k",
                "110.6 k",
                "29.0 G",
                "5m",
                "5m30",
                "2.7k",
                "2k7",
                "2k17",
                "9.2 E",
                "9.2EE"
        };
        Test test = new Test();
        test.printValues(values);
    }

    private void printValues(String[] values) {
        for (String value : values) {
            if (isProperNumber(value)) {
                printValue(value);
            } else {
                System.out.println("Not a proper number");
            }
            System.out.println("===================");
        }
    }

    private void printValue(String value) {
        String lastAlphabet = value.replaceAll("[^a-zA-Z]*$", "")
                                   .replaceAll(".(?!$)", "");
        long multiplier = 1L;

        switch (lastAlphabet.toLowerCase()) {
            case "k":
                multiplier = 1_000L;
                break;
            case "m":
                multiplier = 1_000_000L;
                break;
            case "g":
                multiplier = 1_000_000_000L;
                break;
            case "t":
                multiplier = 1_000_000_000_000L;
                break;
            case "p":
                multiplier = 1_000_000_000_000_000L;
                break;
            case "e":
                multiplier = 1_000_000_000_000_000_000L;
                break;
            default:
                break;
        }

        String[] values = value.split(lastAlphabet);

        if (multiplier == 1) {
            System.out.println("" + values[0]);
        } else {

            double valueMultiplier = Double.parseDouble(values[0]);
            double valueAdder;
            try {
                valueAdder = Double.parseDouble(values[1]);
            } catch (ArrayIndexOutOfBoundsException ex) {
                valueAdder = 0.0d;
            }
            double total = (valueMultiplier * multiplier) + valueAdder;
            System.out.printf("%.0f\n", total);
        }
    }

    private boolean isProperNumber(String value) {
        value = value.replaceAll("\\s+", "");
        String count = value.replaceAll("[.0-9]+", "");
        return count.length() < 2;
    }
}

我在转换之前将值存储在ArrayList中就得到了解决方案,它在osum中工作。谢谢大家的帮助:

不用担心,只需使用简单的JS技巧,将数字四舍五入到小数点后2位即可

function somefunction(count){
    if (count >= 1000) {
        return Math.round((count/1000) * 10)/10+'k';
    } else {
        return count;
    }
}

如果您想增加更多功能,只需在使用Device 1000时增加更多的else,即增加一百万或十亿,只需增加两三个else if,即可使您的代码变得更好。

将k替换为000?如中所示,string.replacek,000?好的,谢谢您的帮助。未提及,但有效值也可以是2k7。只是替换不是一个选项。。。。因为这将对字符串进行盲替换…@arminb no,它将是2.7k。