1 19 20 package jxl.write.biff; 21 22 import common.Logger; 23 import jxl.biff.FormatRecord; 24 25 28 public class NumberFormatRecord extends FormatRecord 29 { 30 private static Logger logger = Logger.getLogger(NumberFormatRecord.class); 31 32 38 protected NumberFormatRecord(String fmt) 39 { 40 super(); 41 42 String fs = fmt; 44 45 fs = replace(fs, "E0", "E+0"); 46 47 fs = trimInvalidChars(fs); 48 49 setFormatString(fs); 50 } 51 52 59 private String trimInvalidChars(String fs) 60 { 61 int firstHash = fs.indexOf('#'); 62 int firstZero = fs.indexOf('0'); 63 int firstValidChar = 0; 64 65 if (firstHash == -1 && firstZero == -1) 66 { 67 return "#.###"; 69 } 70 71 if (firstHash != 0 && firstZero != 0 && 72 firstHash != 1 && firstZero != 1) 73 { 74 firstHash = firstHash == -1?firstHash = Integer.MAX_VALUE:firstHash; 76 firstZero = firstZero == -1?firstZero = Integer.MAX_VALUE:firstZero; 77 firstValidChar = Math.min(firstHash, firstZero); 78 79 StringBuffer tmp = new StringBuffer (); 80 tmp.append(fs.charAt(0)); 81 tmp.append(fs.substring(firstValidChar)); 82 fs = tmp.toString(); 83 } 84 85 int lastHash = fs.lastIndexOf('#'); 87 int lastZero = fs.lastIndexOf('0'); 88 89 if (lastHash == fs.length() || 90 lastZero == fs.length()) 91 { 92 return fs; 93 } 94 95 int lastValidChar = Math.max(lastHash, lastZero); 97 98 while ((fs.length() > lastValidChar + 1) && 100 (fs.charAt(lastValidChar+1) == ')' || 101 (fs.charAt(lastValidChar+1) == '%'))) 102 { 103 lastValidChar++; 104 } 105 106 return fs.substring(0, lastValidChar+1); 107 } 108 } 109 110 111 112 | Popular Tags |