日期:2014-05-20 浏览次数:20711 次
public class Test001 {
public static void main(String[] args) {
output(-4539872316.211212512);
}
static void output(double num) {
// 符号
if (num < 0) {
System.out.print("-");
}
// 整数部分'
long digit = Math.abs((long)num);
if(digit < 1) {
System.out.print("0");
} else {
printDigit(digit);
}
//小数点
System.out.print(".");
// 小数部分'
double decimal = Math.abs(num - (long)num);
if(decimal > 0) {
printDigit(decimal);
} else {
System.out.print("0");
}
System.out.println();
}
static void printDigit(double num) {
if (num > 1) {
printDigit((long)(num / 10));
System.out.print((long)num % 10);
} else if(num > 0 && num < 1) {
long digit = (long)(num * 10);
System.out.print(digit);
printDigit(num * 10 - digit);
}
}
}