2017-06-19 51 views
1

你好我是新來的java任何人都可以幫助我嗎?如何將2k,1m,1g等轉換爲2000,1000000等

該代碼轉換 2000至2k和百萬1米等

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轉化爲百萬

+4

把'k'替換爲'000'?如in,'string.replace(「k」,「000」)'? –

+0

好的感謝幫助。 – Prateek218

+2

未提及,但有效值也可以是'2k7'。 – arminb

回答

1

我得到的解決方案只是通過在轉換之前將值存儲在ArrayList中並且它工作osum。感謝大家的幫助:)

-1
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; 
} 
0

我已經發佈一個代碼一個辦法。需要適當的輸入,因爲不包括適當的驗證。讓我知道它是否適合您的需求。您也可以根據需要添加/修改開關盒。

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; 
    } 
} 
相關問題