1 10 11 package com.triactive.jdo.util; 12 13 import java.math.BigDecimal ; 14 15 16 public class SQLFormat 17 { 18 28 29 public static String format(BigDecimal bd) 30 { 31 String digits = bd.unscaledValue().abs().toString(); 32 int scale = bd.scale(); 33 int len = digits.length(); 34 35 36 while (len > 1 && digits.charAt(len - 1) == '0') 37 { 38 --scale; 39 --len; 40 } 41 42 if (len < digits.length()) 43 digits = digits.substring(0, len); 44 45 StringBuffer sb = new StringBuffer (); 46 47 if (bd.signum() < 0) 48 sb.append('-'); 49 50 int exponent = len - scale; 51 52 if (exponent < 0 || exponent > len) 53 { 54 55 sb.append('.').append(digits).append('E').append(exponent); 56 } 57 else if (exponent == len) 58 { 59 60 sb.append(digits); 61 } 62 else 63 { 64 65 sb.append(digits.substring(0, exponent)).append('.').append(digits.substring(exponent)); 66 } 67 68 return sb.toString(); 69 } 70 } 71 | Popular Tags |