日期:2014-05-20  浏览次数:20701 次

求助java的小数循环程序
哪位高手能帮我写一个java的小数循环程序 然我研究一下

 如将2.5循环为2.555555555

 谢谢了 在线等


------解决方案--------------------
public static void a(float i, int count) {
String ss = "";
String str = String.valueOf(i);
String[] s = new String[2];
int index = str.indexOf(".");
String a1 = "";
String a2 = "";
if(index>=0){
a1 = str.substring(0,index+1);
a2 = str.substring(index+1);
}
for(int j =0;j<count;j++){
a1 += a2;
}System.out.println(a1);

/* s = str.split(".");
for (int j = 0; j < count; j++) {
ss += s[0];
}
System.out.println(ss);*/
}
------解决方案--------------------
long getFmum(float input, int innum)
{
float result = input;
int num = innum;
float snum = (result - (int)result);
int length = getLong(snum);
while(num > 0)
{
result += snum*1e-(num*length);
num--;
}
return result;
}
int getLong(float input)
{
int result;
while(1)
{ if( input*10 != 0 )
{
input = input* 10 - (int)(input* 10)
result++;
}else{ break; }
}
return result;
}
------解决方案--------------------
Java code
public class test4 {
    /**
     * @param args
     */
    private static double getdouble(double s,int c) {
        // TODO Auto-generated method stub
        String str = String.valueOf(s);
        int count = str.indexOf(".");
        if(count!=-1){
            StringBuffer a = new StringBuffer(str.substring(0,count+1));
            String b = str.substring(count+1);
            for(int i = 0 ; i < c;i++){
                a.append(b);
            }
            s = Double.valueOf(a.toString()).doubleValue();
        }
        return s;
    }
    public static void main(String[] args) {
        double s = 32.534;
        s = getdouble(s,3);
        System.out.println(s);
    }
}