算法:带小数的十进制转换为八进制

十进制转换为八进制,带小数的进制转换

public class DecimalToOctal { /** * @param args */ public static int demical_to_octal(double f) { double init_value = f; boolean negative = false; if (f < 0) { negative = true; f = -f; } String str = String.valueOf(f); StringBuilder result0 = new StringBuilder(); StringBuilder result1 = new StringBuilder(); int integer_value; double decimal_fraction; integer_value = Integer.valueOf(str.substring(0, str.indexOf("."))); decimal_fraction = Float.valueOf(str.substring(str.indexOf("."), str .length())); while (integer_value != 0) { result0.append(integer_value % 8); integer_value /= 8; } if (negative) { result0.append("-"); } while (decimal_fraction >= 10e-10) { decimal_fraction = decimal_fraction * 8; int t = (int) (decimal_fraction % 10); result1.append(t); decimal_fraction -= t; } if (String.valueOf(result1).equals("")) { result1.append("0"); } result0 = result0.reverse().append(".").append(result1); System.out.println("(Dec)" + init_value + " = (Oct)" + result0); return 1; } public static void main(String[] args) { // TODO Auto-generated method stub demical_to_octal(10.999); demical_to_octal(10); demical_to_octal(10.1); demical_to_octal(-10.999); demical_to_octal(-10); demical_to_octal(-10.1); demical_to_octal(-7); } }

你可能感兴趣的:(算法,String,Integer,Class)