1 51 package org.apache.fop.pdf; 52 53 public class PDFNumber { 54 55 public static String doubleOut(Double doubleDown) { 56 return doubleOut(doubleDown.doubleValue()); 57 } 58 59 public static String doubleOut(double doubleDown) { 60 StringBuffer p = new StringBuffer (); 61 if (doubleDown < 0) { 62 doubleDown = -doubleDown; 63 p.append("-"); 64 } 65 double trouble = doubleDown % 1; 66 67 if (trouble > 0.950) { 68 p.append((int)doubleDown + 1); 69 } else if (trouble < 0.050) { 70 p.append((int)doubleDown); 71 } else { 72 String doubleString = Double.toString(doubleDown); 73 int decimal = doubleString.indexOf("."); 74 if (decimal != -1) { 75 p.append(doubleString.substring(0, decimal)); 76 77 if ((doubleString.length() - decimal) > 6) { 78 p.append(doubleString.substring(decimal, decimal + 6)); 79 } else { 80 p.append(doubleString.substring(decimal)); 81 } 82 } else { 83 p.append(doubleString); 84 } 85 } 86 return (p.toString()); 87 } 88 89 public static String doubleOut(double doubleDown, int dec) { 90 91 StringBuffer p = new StringBuffer (); 92 if (doubleDown < 0) { 93 doubleDown = -doubleDown; 94 p.append("-"); 95 } 96 double trouble = doubleDown % 1; 97 98 if (trouble > (1.0 - (5.0 / (Math.pow(10.0, dec))))) { 99 p.append((int)doubleDown + 1); 100 } else if (trouble < (5.0 / (Math.pow(10.0, dec)))) { 101 p.append((int)doubleDown); 102 } else { 103 String doubleString = Double.toString(doubleDown); 104 int decimal = doubleString.indexOf("."); 105 if (decimal != -1) { 106 p.append(doubleString.substring(0, decimal)); 107 108 if ((doubleString.length() - decimal) > dec) { 109 p.append(doubleString.substring(decimal, decimal + dec)); 110 } else { 111 p.append(doubleString.substring(decimal)); 112 } 113 } else { 114 p.append(doubleString); 115 } 116 } 117 return (p.toString()); 118 } 119 120 } 121 122 | Popular Tags |