Java: Format number in millions

前端 未结 8 2242
日久生厌
日久生厌 2020-12-10 14:01

Is there a way to use DecimalFormat (or some other standard formatter) to format numbers like this:

1,000,000 => 1.00M

1,234,567 =>

8条回答
  •  忘掉有多难
    2020-12-10 14:52

    For someone looking out there to convert a given digit in human readable form.

    public static String getHumanReadablePriceFromNumber(long number){
    
        if(number >= 1000000000){
            return String.format("%.2fB", number/ 1000000000.0);
        }
    
        if(number >= 1000000){
            return String.format("%.2fM", number/ 1000000.0);
        }
    
        if(number >= 100000){
            return String.format("%.2fL", number/ 100000.0);
        }
    
        if(number >=1000){
            return String.format("%.2fK", number/ 1000.0);
        }
        return String.valueOf(number);
    
    }
    

提交回复
热议问题