Java浮动到字符串

时间:2015-03-24 05:52:44

标签: java string format precision

如何在Java中用两个数字精确地将Float格式化为String?

[需要]

func(12345);//"12000"
func(0.0012345);//"0.0012"

[不工作]

new DecimalFormat("#.00").format(12345);//"12345,00"
new DecimalFormat("#.00").format(0.0012345);",00"

new DecimalFormat("%.2f").format(12345);//"%1234500,2f"
new DecimalFormat("%.2f").format(0.0012345);"%0,2f"

String.format("%.2f",12345));//"12345,00"
String.format("%.2f",0.0012345));//"0,00"

[工作,但不是标准]

static String func(float f,int digits)
{
    int off=(int)Math.log10(f)-digits;
    if(off>=0)
        off++;
    double move=Math.pow(10, off);
    return ""+(float)(Math.round(f/move)*move);
}

func(12345);//"12000"
func(0.0012345);//"0.0012"

我可以使用Java标准库吗?

2 个答案:

答案 0 :(得分:0)

请参阅此代码

public static String func(float num, int dec){
    String numAfter = String.valueOf(num);
    String[] splitedStr = numAfter.split("\\.");
    if(dec<=splitedStr[1].length()){
        if(dec==0){
            return splitedStr[0];
        }
        else{
            return splitedStr[0] + "." + splitedStr[1].substring(0, dec);
        }

    }
    else
    {
        for(int i = 0;i<dec - splitedStr[1].length();i++){
            numAfter = numAfter + "0";
        }
        return numAfter;
    }

}

所以我认为我们应该首先将它们转换为String。
您可以删除静态以使其与非静态方法一起使用 请尝试使用此代码,如果结果是您的预期结果。我不明白你的问题。
结果

123450.0000000000 =&gt; 120000个
12345.0000000000 =&gt; 12000个
1234.5000000000 =&gt; 1200
123.4499969482 =&gt; 120个
12.3450002670&gt; 12个
1.2345000505 =&gt; 1.2
0.1234500036 =&gt; 0.12
0.0123450002 =&gt; 0.012
0.0012345000 =&gt; 0.0012000000 =&gt; 0.0012
0.0003232155 =&gt; 0.0003200000 =&gt; 0.00032


<强>代码

public static String func(float num, int dec){      
    num = Float.valueOf(num);
    String numAfter = String.format("%.10f", num);      
    String result = "";
    int count = 0;
    boolean startToCount = false;
    for(int i = 0;i<numAfter.length();i++){ 
        if(numAfter.charAt(i)!='0'){
            count++;
            startToCount = true;
            if(count>dec){
                startToCount = false;
            }           
        }           
        if(startToCount){
            if(numAfter.charAt(i)=='.'){
                count--;
            }   
            result = result + numAfter.charAt(i);
        }
        else{
            if(numAfter.charAt(i)=='.'){
                result = result + ".";
            }
            else{
                result = result + "0";
            }   
        }
    }
    System.out.print(numAfter + " => " + result + " => "); // Just to Debug
    int lengCount = result.length();
    for(int j = numAfter.length()-1;j>=0;j--){
        if(numAfter.charAt(j)=='.'||result.charAt(j)!='0'){
            break;
        }
        else{
            lengCount--;
        }
    }
    result = result.substring(0, lengCount);
    if(result.charAt(result.length()-1)=='.'){
        result = result + "0";
    }       
    return result;
}

答案 1 :(得分:0)

Myfunc: f = 12345.0; func = 12000.0
Myfunc: f = 1234.5; func = 1200.0
Myfunc: f = 123.45; func = 120.0
Myfunc: f = 12.344999; func = 12.3
Myfunc: f = 1.2344999; func = 1.23
Myfunc: f = 0.123449996; func = 0.12
Myfunc: f = 0.012344999; func = 0.012
Myfunc: f = 0.0012345; func = 0.0012

YouFunc_1: f = 12345.0; func = 12345.00
YouFunc_1: f = 1234.5; func = 1234.50
YouFunc_1: f = 123.45; func = 123.45
YouFunc_1: f = 12.344999; func = 12.34
YouFunc_1: f = 1.2344999; func = 1.23
YouFunc_1: f = 0.123449996; func = 0.12
YouFunc_1: f = 0.012344999; func = 0.01
YouFunc_1: f = 0.0012345; func = 0.00

YouFunc_2: f = 12345.0; func = 12
YouFunc_2: f = 1234.5; func = 12
YouFunc_2: f = 123.45; func = 12
YouFunc_2: f = 12.344999; func = 12
YouFunc_2: f = 1.2344999; func = 1,
YouFunc_2: f = 0.123449996; func = 0,1
YouFunc_2: f = 0.012344999; func = 0,01
YouFunc_2: f = 0.0012345; func = 0,001
thienkhoi tran,谢谢你,不用担心。据我所知,Java无法使用标准功能。