1 31 package org.pdfbox.cos; 32 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 36 import java.text.DecimalFormat ; 37 import java.text.DecimalFormatSymbols ; 38 import java.text.NumberFormat ; 39 40 import org.pdfbox.exceptions.COSVisitorException; 41 42 48 public class COSFloat extends COSNumber 49 { 50 private float value; 51 52 57 public COSFloat( float aFloat ) 58 { 59 value = aFloat; 60 } 61 62 69 public COSFloat( String aFloat ) throws IOException 70 { 71 try 72 { 73 value = Float.parseFloat( aFloat ); 74 } 75 catch( NumberFormatException e ) 76 { 77 throw new IOException ( "Error expected floating point number actual='" +aFloat + "'" ); 78 } 79 } 80 81 86 public void setValue( float floatValue ) 87 { 88 value = floatValue; 89 } 90 91 96 public float floatValue() 97 { 98 return value; 99 } 100 101 106 public double doubleValue() 107 { 108 return value; 109 } 110 111 116 public long longValue() 117 { 118 return (long)value; 119 } 120 121 126 public int intValue() 127 { 128 return (int)value; 129 } 130 131 134 public boolean equals( Object o ) 135 { 136 return o instanceof COSFloat && Float.floatToIntBits(((COSFloat)o).value) == Float.floatToIntBits(value); 137 } 138 139 142 public int hashCode() 143 { 144 return Float.floatToIntBits(value); 145 } 146 147 150 public String toString() 151 { 152 return "COSFloat{" + value + "}"; 153 } 154 155 162 public Object accept(ICOSVisitor visitor) throws COSVisitorException 163 { 164 return visitor.visitFromFloat(this); 165 } 166 167 173 public void writePDF( OutputStream output ) throws IOException 174 { 175 DecimalFormat formatDecimal = (DecimalFormat )NumberFormat.getNumberInstance(); 176 formatDecimal.setMaximumFractionDigits( 10 ); 177 formatDecimal.setGroupingUsed( false ); 178 DecimalFormatSymbols symbols = formatDecimal.getDecimalFormatSymbols(); 179 symbols.setDecimalSeparator( '.' ); 180 formatDecimal.setDecimalFormatSymbols( symbols ); 181 output.write(formatDecimal.format( value ).getBytes()); 182 } 183 } | Popular Tags |