1 21 package com.db4o.handlers; 22 23 import java.math.*; 24 25 import com.db4o.*; 26 27 30 35 public class NetDecimal extends NetSimpleTypeHandler{ 36 37 private static final BigInteger BYTESHIFT_FACTOR=new BigInteger("100",16); 38 39 private static final BigInteger ZERO = new BigInteger("0", 16); 40 41 private static final BigDecimal TEN = new BigDecimal("10"); 42 43 public NetDecimal(YapStream stream) { 44 super(stream, 21, 16); 45 } 46 47 public String toString(byte[] bytes) { 48 BigInteger mantissa=ZERO; 49 for(int blockoffset=8;blockoffset>=0;blockoffset-=4) { 50 for(int byteidx=0;byteidx<4;byteidx++) { 51 mantissa=mantissa.multiply(BYTESHIFT_FACTOR); 52 int idx=blockoffset+byteidx; 53 mantissa=mantissa.add(new BigInteger(String.valueOf(bytes[idx]&0xff),10)); 54 } 55 } 56 57 int exponent = - bytes[13]&0x1f; 59 60 boolean negative=bytes[12]!=0; 61 62 BigDecimal result=new BigDecimal(mantissa); 63 64 if(exponent < 0) { 65 for (int i = exponent; i < 0; i++) { 66 result = result.divide(TEN, BigDecimal.ROUND_HALF_DOWN); 67 } 68 }else { 69 for (int i = 0; i < exponent; i++) { 70 result = result.multiply(TEN); 71 } 72 } 73 74 if(negative) { 75 result=result.negate(); 76 } 77 return result.toString(); 78 } 79 } 80 | Popular Tags |