You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NumberToTextConverter.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.util;
  16. /**
  17. * Excel converts numbers to text with different rules to those of java, so
  18. * {@code Double.toString(value)} won't do.
  19. * <ul>
  20. * <li>No more than 15 significant figures are output (java does 18).</li>
  21. * <li>The sign char for the exponent is included even if positive</li>
  22. * <li>Special values ({@code NaN} and {@code Infinity}) get rendered like the ordinary
  23. * number that the bit pattern represents.</li>
  24. * <li>Denormalised values (between &plusmn;2<sup>-1074</sup> and &plusmn;2<sup>-1022</sup>
  25. * are displayed as "0"</li>
  26. * </ul>
  27. * IEEE 64-bit Double Rendering Comparison
  28. *
  29. * <table>
  30. * <caption>IEEE 64-bit Double Rendering Comparison</caption>
  31. * <tr><th>Raw bits</th><th>Java</th><th>Excel</th></tr>
  32. *
  33. * <tr><td>0x0000000000000000L</td><td>0.0</td><td>0</td></tr>
  34. * <tr><td>0x3FF0000000000000L</td><td>1.0</td><td>1</td></tr>
  35. * <tr><td>0x3FF00068DB8BAC71L</td><td>1.0001</td><td>1.0001</td></tr>
  36. * <tr><td>0x4087A00000000000L</td><td>756.0</td><td>756</td></tr>
  37. * <tr><td>0x401E3D70A3D70A3DL</td><td>7.56</td><td>7.56</td></tr>
  38. * <tr><td>0x405EDD3C07FB4C99L</td><td>123.45678901234568</td><td>123.456789012346</td></tr>
  39. * <tr><td>0x4132D687E3DF2180L</td><td>1234567.8901234567</td><td>1234567.89012346</td></tr>
  40. * <tr><td>0x3EE9E409302678BAL</td><td>1.2345678901234568E-5</td><td>1.23456789012346E-05</td></tr>
  41. * <tr><td>0x3F202E85BE180B74L</td><td>1.2345678901234567E-4</td><td>0.000123456789012346</td></tr>
  42. * <tr><td>0x3F543A272D9E0E51L</td><td>0.0012345678901234567</td><td>0.00123456789012346</td></tr>
  43. * <tr><td>0x3F8948B0F90591E6L</td><td>0.012345678901234568</td><td>0.0123456789012346</td></tr>
  44. * <tr><td>0x3EE9E409301B5A02L</td><td>1.23456789E-5</td><td>0.0000123456789</td></tr>
  45. * <tr><td>0x3E6E7D05BDABDE50L</td><td>5.6789012345E-8</td><td>0.000000056789012345</td></tr>
  46. * <tr><td>0x3E6E7D05BDAD407EL</td><td>5.67890123456E-8</td><td>5.67890123456E-08</td></tr>
  47. * <tr><td>0x3E6E7D06029F18BEL</td><td>5.678902E-8</td><td>0.00000005678902</td></tr>
  48. * <tr><td>0x2BCB5733CB32AE6EL</td><td>9.999999999999123E-98</td><td>9.99999999999912E-98</td></tr>
  49. * <tr><td>0x2B617F7D4ED8C59EL</td><td>1.0000000000001235E-99</td><td>1.0000000000001E-99</td></tr>
  50. * <tr><td>0x0036319916D67853L</td><td>1.2345678901234578E-307</td><td>1.2345678901235E-307</td></tr>
  51. * <tr><td>0x359DEE7A4AD4B81FL</td><td>2.0E-50</td><td>2E-50</td></tr>
  52. * <tr><td>0x41678C29DCD6E9E0L</td><td>1.2345678901234567E7</td><td>12345678.9012346</td></tr>
  53. * <tr><td>0x42A674E79C5FE523L</td><td>1.2345678901234568E13</td><td>12345678901234.6</td></tr>
  54. * <tr><td>0x42DC12218377DE6BL</td><td>1.2345678901234567E14</td><td>123456789012346</td></tr>
  55. * <tr><td>0x43118B54F22AEB03L</td><td>1.2345678901234568E15</td><td>1234567890123460</td></tr>
  56. * <tr><td>0x43E56A95319D63E1L</td><td>1.2345678901234567E19</td><td>12345678901234600000</td></tr>
  57. * <tr><td>0x441AC53A7E04BCDAL</td><td>1.2345678901234568E20</td><td>1.23456789012346E+20</td></tr>
  58. * <tr><td>0xC3E56A95319D63E1L</td><td>-1.2345678901234567E19</td><td>-12345678901234600000</td></tr>
  59. * <tr><td>0xC41AC53A7E04BCDAL</td><td>-1.2345678901234568E20</td><td>-1.23456789012346E+20</td></tr>
  60. * <tr><td>0x54820FE0BA17F46DL</td><td>1.2345678901234577E99</td><td>1.2345678901235E+99</td></tr>
  61. * <tr><td>0x54B693D8E89DF188L</td><td>1.2345678901234576E100</td><td>1.2345678901235E+100</td></tr>
  62. * <tr><td>0x4A611B0EC57E649AL</td><td>2.0E50</td><td>2E+50</td></tr>
  63. * <tr><td>0x7FEFFFFFFFFFFFFFL</td><td>1.7976931348623157E308</td><td>1.7976931348623E+308</td></tr>
  64. * <tr><td>0x0010000000000000L</td><td>2.2250738585072014E-308</td><td>2.2250738585072E-308</td></tr>
  65. * <tr><td>0x000FFFFFFFFFFFFFL</td><td>2.225073858507201E-308</td><td>0</td></tr>
  66. * <tr><td>0x0000000000000001L</td><td>4.9E-324</td><td>0</td></tr>
  67. * <tr><td>0x7FF0000000000000L</td><td>Infinity</td><td>1.7976931348623E+308</td></tr>
  68. * <tr><td>0xFFF0000000000000L</td><td>-Infinity</td><td>1.7976931348623E+308</td></tr>
  69. * <tr><td>0x441AC7A08EAD02F2L</td><td>1.234999999999999E20</td><td>1.235E+20</td></tr>
  70. * <tr><td>0x40FE26BFFFFFFFF9L</td><td>123499.9999999999</td><td>123500</td></tr>
  71. * <tr><td>0x3E4A857BFB2F2809L</td><td>1.234999999999999E-8</td><td>0.00000001235</td></tr>
  72. * <tr><td>0x3BCD291DEF868C89L</td><td>1.234999999999999E-20</td><td>1.235E-20</td></tr>
  73. * <tr><td>0x444B1AE4D6E2EF4FL</td><td>9.999999999999999E20</td><td>1E+21</td></tr>
  74. * <tr><td>0x412E847FFFFFFFFFL</td><td>999999.9999999999</td><td>1000000</td></tr>
  75. * <tr><td>0x3E45798EE2308C39L</td><td>9.999999999999999E-9</td><td>0.00000001</td></tr>
  76. * <tr><td>0x3C32725DD1D243ABL</td><td>9.999999999999999E-19</td><td>0.000000000000000001</td></tr>
  77. * <tr><td>0x3BFD83C94FB6D2ABL</td><td>9.999999999999999E-20</td><td>1E-19</td></tr>
  78. * <tr><td>0xC44B1AE4D6E2EF4FL</td><td>-9.999999999999999E20</td><td>-1E+21</td></tr>
  79. * <tr><td>0xC12E847FFFFFFFFFL</td><td>-999999.9999999999</td><td>-1000000</td></tr>
  80. * <tr><td>0xBE45798EE2308C39L</td><td>-9.999999999999999E-9</td><td>-0.00000001</td></tr>
  81. * <tr><td>0xBC32725DD1D243ABL</td><td>-9.999999999999999E-19</td><td>-0.000000000000000001</td></tr>
  82. * <tr><td>0xBBFD83C94FB6D2ABL</td><td>-9.999999999999999E-20</td><td>-1E-19</td></tr>
  83. * <tr><td>0xFFFF0420003C0000L</td><td>NaN</td><td>3.484840871308E+308</td></tr>
  84. * <tr><td>0x7FF8000000000000L</td><td>NaN</td><td>2.6965397022935E+308</td></tr>
  85. * <tr><td>0x7FFF0420003C0000L</td><td>NaN</td><td>3.484840871308E+308</td></tr>
  86. * <tr><td>0xFFF8000000000000L</td><td>NaN</td><td>2.6965397022935E+308</td></tr>
  87. * <tr><td>0xFFFF0AAAAAAAAAAAL</td><td>NaN</td><td>3.4877119413344E+308</td></tr>
  88. * <tr><td>0x7FF80AAAAAAAAAAAL</td><td>NaN</td><td>2.7012211948322E+308</td></tr>
  89. * <tr><td>0xFFFFFFFFFFFFFFFFL</td><td>NaN</td><td>3.5953862697246E+308</td></tr>
  90. * <tr><td>0x7FFFFFFFFFFFFFFFL</td><td>NaN</td><td>3.5953862697246E+308</td></tr>
  91. * <tr><td>0xFFF7FFFFFFFFFFFFL</td><td>NaN</td><td>2.6965397022935E+308</td></tr>
  92. * </table>
  93. *
  94. * <b>Note</b>:
  95. * Excel has inconsistent rules for the following numeric operations:
  96. * <ul>
  97. * <li>Conversion to string (as handled here)</li>
  98. * <li>Rendering numerical quantities in the cell grid.</li>
  99. * <li>Conversion from text</li>
  100. * <li>General arithmetic</li>
  101. * </ul>
  102. * Excel's text to number conversion is not a true <i>inverse</i> of this operation. The
  103. * allowable ranges are different. Some numbers that don't correctly convert to text actually
  104. * <b>do</b> get handled properly when used in arithmetic evaluations.
  105. */
  106. public final class NumberToTextConverter {
  107. private static final long EXCEL_NAN_BITS = 0xFFFF0420003C0000L;
  108. private static final int MAX_TEXT_LEN = 20;
  109. private NumberToTextConverter() {
  110. // no instances of this class
  111. }
  112. /**
  113. * Converts the supplied {@code value} to the text representation that Excel would give if
  114. * the value were to appear in an unformatted cell, or as a literal number in a formula.<br>
  115. * Note - the results from this method differ slightly from those of {@code Double.toString()}
  116. * In some special cases Excel behaves quite differently. This function attempts to reproduce
  117. * those results.
  118. */
  119. public static String toText(double value) {
  120. return rawDoubleBitsToText(Double.doubleToLongBits(value));
  121. }
  122. /* package */ static String rawDoubleBitsToText(long pRawBits) {
  123. long rawBits = pRawBits;
  124. boolean isNegative = rawBits < 0; // sign bit is in the same place for long and double
  125. if (isNegative) {
  126. rawBits &= 0x7FFFFFFFFFFFFFFFL;
  127. }
  128. if (rawBits == 0) {
  129. return isNegative ? "-0" : "0";
  130. }
  131. ExpandedDouble ed = new ExpandedDouble(rawBits);
  132. if (ed.getBinaryExponent() < -1022) {
  133. // value is 'denormalised' which means it is less than 2^-1022
  134. // excel displays all these numbers as zero, even though calculations work OK
  135. return isNegative ? "-0" : "0";
  136. }
  137. if (ed.getBinaryExponent() == 1024) {
  138. // Special number NaN /Infinity
  139. // Normally one would not create HybridDecimal objects from these values
  140. // except in these cases Excel really tries to render them as if they were normal numbers
  141. if(rawBits == EXCEL_NAN_BITS) {
  142. return "3.484840871308E+308";
  143. }
  144. // This is where excel really gets it wrong
  145. // Special numbers like Infinity and NaN are interpreted according to
  146. // the standard rules below.
  147. isNegative = false; // except that the sign bit is ignored
  148. }
  149. NormalisedDecimal nd = ed.normaliseBaseTen();
  150. StringBuilder sb = new StringBuilder(MAX_TEXT_LEN+1);
  151. if (isNegative) {
  152. sb.append('-');
  153. }
  154. convertToText(sb, nd);
  155. return sb.toString();
  156. }
  157. private static void convertToText(StringBuilder sb, NormalisedDecimal pnd) {
  158. NormalisedDecimal rnd = pnd.roundUnits();
  159. int decExponent = rnd.getDecimalExponent();
  160. String decimalDigits;
  161. if (Math.abs(decExponent)>98) {
  162. decimalDigits = rnd.getSignificantDecimalDigitsLastDigitRounded();
  163. if (decimalDigits.length() == 16) {
  164. // rounding caused carry
  165. decExponent++;
  166. }
  167. } else {
  168. decimalDigits = rnd.getSignificantDecimalDigits();
  169. }
  170. int countSigDigits = countSignifantDigits(decimalDigits);
  171. if (decExponent < 0) {
  172. formatLessThanOne(sb, decimalDigits, decExponent, countSigDigits);
  173. } else {
  174. formatGreaterThanOne(sb, decimalDigits, decExponent, countSigDigits);
  175. }
  176. }
  177. private static void formatLessThanOne(StringBuilder sb, String decimalDigits, int decExponent,
  178. int countSigDigits) {
  179. int nLeadingZeros = -decExponent - 1;
  180. int normalLength = 2 + nLeadingZeros + countSigDigits; // 2 == "0.".length()
  181. if (needsScientificNotation(normalLength)) {
  182. sb.append(decimalDigits.charAt(0));
  183. if (countSigDigits > 1) {
  184. sb.append('.');
  185. sb.append(decimalDigits.subSequence(1, countSigDigits));
  186. }
  187. sb.append("E-");
  188. appendExp(sb, -decExponent);
  189. return;
  190. }
  191. sb.append("0.");
  192. for (int i=nLeadingZeros; i>0; i--) {
  193. sb.append('0');
  194. }
  195. sb.append(decimalDigits.subSequence(0, countSigDigits));
  196. }
  197. private static void formatGreaterThanOne(StringBuilder sb, String decimalDigits, int decExponent, int countSigDigits) {
  198. if (decExponent > 19) {
  199. // scientific notation
  200. sb.append(decimalDigits.charAt(0));
  201. if (countSigDigits>1) {
  202. sb.append('.');
  203. sb.append(decimalDigits.subSequence(1, countSigDigits));
  204. }
  205. sb.append("E+");
  206. appendExp(sb, decExponent);
  207. return;
  208. }
  209. int nFractionalDigits = countSigDigits - decExponent - 1;
  210. if (nFractionalDigits > 0) {
  211. sb.append(decimalDigits.subSequence(0, decExponent+1));
  212. sb.append('.');
  213. sb.append(decimalDigits.subSequence(decExponent+1, countSigDigits));
  214. return;
  215. }
  216. sb.append(decimalDigits.subSequence(0, countSigDigits));
  217. for (int i=-nFractionalDigits; i>0; i--) {
  218. sb.append('0');
  219. }
  220. }
  221. private static boolean needsScientificNotation(int nDigits) {
  222. return nDigits > MAX_TEXT_LEN;
  223. }
  224. private static int countSignifantDigits(String sb) {
  225. int result=sb.length()-1;
  226. while(sb.charAt(result) == '0') {
  227. result--;
  228. if(result < 0) {
  229. throw new RuntimeException("No non-zero digits found");
  230. }
  231. }
  232. return result + 1;
  233. }
  234. private static void appendExp(StringBuilder sb, int val) {
  235. if(val < 10) {
  236. sb.append('0');
  237. sb.append((char)('0' + val));
  238. return;
  239. }
  240. sb.append(val);
  241. }
  242. }