NumberFormat.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Style
  23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_Style_NumberFormat
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Style_NumberFormat implements PHPExcel_IComparable
  35. {
  36. /* Pre-defined formats */
  37. const FORMAT_GENERAL = 'General';
  38. const FORMAT_TEXT = '@';
  39. const FORMAT_NUMBER = '0';
  40. const FORMAT_NUMBER_00 = '0.00';
  41. const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00';
  42. const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-';
  43. const FORMAT_PERCENTAGE = '0%';
  44. const FORMAT_PERCENTAGE_00 = '0.00%';
  45. const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd';
  46. const FORMAT_DATE_YYYYMMDD = 'yy-mm-dd';
  47. const FORMAT_DATE_DDMMYYYY = 'dd/mm/yy';
  48. const FORMAT_DATE_DMYSLASH = 'd/m/y';
  49. const FORMAT_DATE_DMYMINUS = 'd-m-y';
  50. const FORMAT_DATE_DMMINUS = 'd-m';
  51. const FORMAT_DATE_MYMINUS = 'm-y';
  52. const FORMAT_DATE_XLSX14 = 'mm-dd-yy';
  53. const FORMAT_DATE_XLSX15 = 'd-mmm-yy';
  54. const FORMAT_DATE_XLSX16 = 'd-mmm';
  55. const FORMAT_DATE_XLSX17 = 'mmm-yy';
  56. const FORMAT_DATE_XLSX22 = 'm/d/yy h:mm';
  57. const FORMAT_DATE_DATETIME = 'd/m/y h:mm';
  58. const FORMAT_DATE_TIME1 = 'h:mm AM/PM';
  59. const FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM';
  60. const FORMAT_DATE_TIME3 = 'h:mm';
  61. const FORMAT_DATE_TIME4 = 'h:mm:ss';
  62. const FORMAT_DATE_TIME5 = 'mm:ss';
  63. const FORMAT_DATE_TIME6 = 'h:mm:ss';
  64. const FORMAT_DATE_TIME7 = 'i:s.S';
  65. const FORMAT_DATE_TIME8 = 'h:mm:ss;@';
  66. const FORMAT_DATE_YYYYMMDDSLASH = 'yy/mm/dd;@';
  67. const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-';
  68. const FORMAT_CURRENCY_USD = '$#,##0_-';
  69. const FORMAT_CURRENCY_EUR_SIMPLE = '[$EUR ]#,##0.00_-';
  70. /**
  71. * Excel built-in number formats
  72. *
  73. * @var array
  74. */
  75. private static $_builtInFormats;
  76. /**
  77. * Excel built-in number formats (flipped, for faster lookups)
  78. *
  79. * @var array
  80. */
  81. private static $_flippedBuiltInFormats;
  82. /**
  83. * Format Code
  84. *
  85. * @var string
  86. */
  87. private $_formatCode = PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  88. /**
  89. * Built-in format Code
  90. *
  91. * @var string
  92. */
  93. private $_builtInFormatCode = 0;
  94. /**
  95. * Parent Borders
  96. *
  97. * @var _parentPropertyName string
  98. */
  99. private $_parentPropertyName;
  100. /**
  101. * Supervisor?
  102. *
  103. * @var boolean
  104. */
  105. private $_isSupervisor;
  106. /**
  107. * Parent. Only used for supervisor
  108. *
  109. * @var PHPExcel_Style
  110. */
  111. private $_parent;
  112. /**
  113. * Create a new PHPExcel_Style_NumberFormat
  114. */
  115. public function __construct($isSupervisor = false)
  116. {
  117. // Supervisor?
  118. $this->_isSupervisor = $isSupervisor;
  119. }
  120. /**
  121. * Bind parent. Only used for supervisor
  122. *
  123. * @param PHPExcel_Style $parent
  124. * @return PHPExcel_Style_NumberFormat
  125. */
  126. public function bindParent($parent)
  127. {
  128. $this->_parent = $parent;
  129. }
  130. /**
  131. * Is this a supervisor or a real style component?
  132. *
  133. * @return boolean
  134. */
  135. public function getIsSupervisor()
  136. {
  137. return $this->_isSupervisor;
  138. }
  139. /**
  140. * Get the shared style component for the currently active cell in currently active sheet.
  141. * Only used for style supervisor
  142. *
  143. * @return PHPExcel_Style_NumberFormat
  144. */
  145. public function getSharedComponent()
  146. {
  147. return $this->_parent->getSharedComponent()->getNumberFormat();
  148. }
  149. /**
  150. * Get the currently active sheet. Only used for supervisor
  151. *
  152. * @return PHPExcel_Worksheet
  153. */
  154. public function getActiveSheet()
  155. {
  156. return $this->_parent->getActiveSheet();
  157. }
  158. /**
  159. * Get the currently active cell coordinate in currently active sheet.
  160. * Only used for supervisor
  161. *
  162. * @return string E.g. 'A1'
  163. */
  164. public function getSelectedCells()
  165. {
  166. return $this->getActiveSheet()->getSelectedCells();
  167. }
  168. /**
  169. * Get the currently active cell coordinate in currently active sheet.
  170. * Only used for supervisor
  171. *
  172. * @return string E.g. 'A1'
  173. */
  174. public function getActiveCell()
  175. {
  176. return $this->getActiveSheet()->getActiveCell();
  177. }
  178. /**
  179. * Build style array from subcomponents
  180. *
  181. * @param array $array
  182. * @return array
  183. */
  184. public function getStyleArray($array)
  185. {
  186. return array('numberformat' => $array);
  187. }
  188. /**
  189. * Apply styles from array
  190. *
  191. * <code>
  192. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray(
  193. * array(
  194. * 'code' => PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE
  195. * )
  196. * );
  197. * </code>
  198. *
  199. * @param array $pStyles Array containing style information
  200. * @throws Exception
  201. * @return PHPExcel_Style_NumberFormat
  202. */
  203. public function applyFromArray($pStyles = null)
  204. {
  205. if (is_array($pStyles)) {
  206. if ($this->_isSupervisor) {
  207. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  208. } else {
  209. if (array_key_exists('code', $pStyles)) {
  210. $this->setFormatCode($pStyles['code']);
  211. }
  212. }
  213. } else {
  214. throw new Exception("Invalid style array passed.");
  215. }
  216. return $this;
  217. }
  218. /**
  219. * Get Format Code
  220. *
  221. * @return string
  222. */
  223. public function getFormatCode()
  224. {
  225. if ($this->_isSupervisor) {
  226. return $this->getSharedComponent()->getFormatCode();
  227. }
  228. if ($this->_builtInFormatCode !== false)
  229. {
  230. return self::builtInFormatCode($this->_builtInFormatCode);
  231. }
  232. return $this->_formatCode;
  233. }
  234. /**
  235. * Set Format Code
  236. *
  237. * @param string $pValue
  238. * @return PHPExcel_Style_NumberFormat
  239. */
  240. public function setFormatCode($pValue = PHPExcel_Style_NumberFormat::FORMAT_GENERAL)
  241. {
  242. if ($pValue == '') {
  243. $pValue = PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  244. }
  245. if ($this->_isSupervisor) {
  246. $styleArray = $this->getStyleArray(array('code' => $pValue));
  247. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  248. } else {
  249. $this->_formatCode = $pValue;
  250. $this->_builtInFormatCode = self::builtInFormatCodeIndex($pValue);
  251. }
  252. return $this;
  253. }
  254. /**
  255. * Get Built-In Format Code
  256. *
  257. * @return int
  258. */
  259. public function getBuiltInFormatCode()
  260. {
  261. if ($this->_isSupervisor) {
  262. return $this->getSharedComponent()->getBuiltInFormatCode();
  263. }
  264. return $this->_builtInFormatCode;
  265. }
  266. /**
  267. * Set Built-In Format Code
  268. *
  269. * @param int $pValue
  270. * @return PHPExcel_Style_NumberFormat
  271. */
  272. public function setBuiltInFormatCode($pValue = 0)
  273. {
  274. if ($this->_isSupervisor) {
  275. $styleArray = $this->getStyleArray(array('code' => self::builtInFormatCode($pValue)));
  276. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  277. } else {
  278. $this->_builtInFormatCode = $pValue;
  279. $this->_formatCode = self::builtInFormatCode($pValue);
  280. }
  281. return $this;
  282. }
  283. /**
  284. * Fill built-in format codes
  285. */
  286. private static function fillBuiltInFormatCodes()
  287. {
  288. // Built-in format codes
  289. if (is_null(self::$_builtInFormats)) {
  290. self::$_builtInFormats = array();
  291. // General
  292. self::$_builtInFormats[0] = PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  293. self::$_builtInFormats[1] = '0';
  294. self::$_builtInFormats[2] = '0.00';
  295. self::$_builtInFormats[3] = '#,##0';
  296. self::$_builtInFormats[4] = '#,##0.00';
  297. self::$_builtInFormats[9] = '0%';
  298. self::$_builtInFormats[10] = '0.00%';
  299. self::$_builtInFormats[11] = '0.00E+00';
  300. self::$_builtInFormats[12] = '# ?/?';
  301. self::$_builtInFormats[13] = '# ??/??';
  302. self::$_builtInFormats[14] = 'mm-dd-yy';
  303. self::$_builtInFormats[15] = 'd-mmm-yy';
  304. self::$_builtInFormats[16] = 'd-mmm';
  305. self::$_builtInFormats[17] = 'mmm-yy';
  306. self::$_builtInFormats[18] = 'h:mm AM/PM';
  307. self::$_builtInFormats[19] = 'h:mm:ss AM/PM';
  308. self::$_builtInFormats[20] = 'h:mm';
  309. self::$_builtInFormats[21] = 'h:mm:ss';
  310. self::$_builtInFormats[22] = 'm/d/yy h:mm';
  311. self::$_builtInFormats[37] = '#,##0 ;(#,##0)';
  312. self::$_builtInFormats[38] = '#,##0 ;[Red](#,##0)';
  313. self::$_builtInFormats[39] = '#,##0.00;(#,##0.00)';
  314. self::$_builtInFormats[40] = '#,##0.00;[Red](#,##0.00)';
  315. self::$_builtInFormats[44] = '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)';
  316. self::$_builtInFormats[45] = 'mm:ss';
  317. self::$_builtInFormats[46] = '[h]:mm:ss';
  318. self::$_builtInFormats[47] = 'mmss.0';
  319. self::$_builtInFormats[48] = '##0.0E+0';
  320. self::$_builtInFormats[49] = '@';
  321. // CHT
  322. self::$_builtInFormats[27] = '[$-404]e/m/d';
  323. self::$_builtInFormats[30] = 'm/d/yy';
  324. self::$_builtInFormats[36] = '[$-404]e/m/d';
  325. self::$_builtInFormats[50] = '[$-404]e/m/d';
  326. self::$_builtInFormats[57] = '[$-404]e/m/d';
  327. // THA
  328. self::$_builtInFormats[59] = 't0';
  329. self::$_builtInFormats[60] = 't0.00';
  330. self::$_builtInFormats[61] = 't#,##0';
  331. self::$_builtInFormats[62] = 't#,##0.00';
  332. self::$_builtInFormats[67] = 't0%';
  333. self::$_builtInFormats[68] = 't0.00%';
  334. self::$_builtInFormats[69] = 't# ?/?';
  335. self::$_builtInFormats[70] = 't# ??/??';
  336. // Flip array (for faster lookups)
  337. self::$_flippedBuiltInFormats = array_flip(self::$_builtInFormats);
  338. }
  339. }
  340. /**
  341. * Get built-in format code
  342. *
  343. * @param int $pIndex
  344. * @return string
  345. */
  346. public static function builtInFormatCode($pIndex)
  347. {
  348. // Clean parameter
  349. $pIndex = intval($pIndex);
  350. // Ensure built-in format codes are available
  351. self::fillBuiltInFormatCodes();
  352. // Lookup format code
  353. if (isset(self::$_builtInFormats[$pIndex])) {
  354. return self::$_builtInFormats[$pIndex];
  355. }
  356. return '';
  357. }
  358. /**
  359. * Get built-in format code index
  360. *
  361. * @param string $formatCode
  362. * @return int|boolean
  363. */
  364. public static function builtInFormatCodeIndex($formatCode)
  365. {
  366. // Ensure built-in format codes are available
  367. self::fillBuiltInFormatCodes();
  368. // Lookup format code
  369. if (isset(self::$_flippedBuiltInFormats[$formatCode])) {
  370. return self::$_flippedBuiltInFormats[$formatCode];
  371. }
  372. return false;
  373. }
  374. /**
  375. * Get hash code
  376. *
  377. * @return string Hash code
  378. */
  379. public function getHashCode()
  380. {
  381. if ($this->_isSupervisor) {
  382. return $this->getSharedComponent()->getHashCode();
  383. }
  384. return md5(
  385. $this->_formatCode
  386. . $this->_builtInFormatCode
  387. . __CLASS__
  388. );
  389. }
  390. /**
  391. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  392. */
  393. public function __clone()
  394. {
  395. $vars = get_object_vars($this);
  396. foreach ($vars as $key => $value) {
  397. if ((is_object($value)) && ($key != '_parent')) {
  398. $this->$key = clone $value;
  399. } else {
  400. $this->$key = $value;
  401. }
  402. }
  403. }
  404. private static $_dateFormatReplacements = array(
  405. // first remove escapes related to non-format characters
  406. '\\' => '',
  407. // 12-hour suffix
  408. 'am/pm' => 'A',
  409. // 4-digit year
  410. 'yyyy' => 'Y',
  411. // 2-digit year
  412. 'yy' => 'y',
  413. // first letter of month - no php equivalent
  414. 'mmmmm' => 'M',
  415. // full month name
  416. 'mmmm' => 'F',
  417. // short month name
  418. 'mmm' => 'M',
  419. // mm is minutes if time or month w/leading zero
  420. ':mm' => ':i',
  421. // month leading zero
  422. 'mm' => 'm',
  423. // month no leading zero
  424. 'm' => 'n',
  425. // full day of week name
  426. 'dddd' => 'l',
  427. // short day of week name
  428. 'ddd' => 'D',
  429. // days leading zero
  430. 'dd' => 'd',
  431. // days no leading zero
  432. 'd' => 'j',
  433. // seconds
  434. 'ss' => 's',
  435. // fractional seconds - no php equivalent
  436. '.s' => ''
  437. );
  438. private static $_dateFormatReplacements24 = array(
  439. 'hh' => 'H',
  440. 'h' => 'G'
  441. );
  442. private static $_dateFormatReplacements12 = array(
  443. 'hh' => 'h',
  444. 'h' => 'g'
  445. );
  446. /**
  447. * Convert a value in a pre-defined format to a PHP string
  448. *
  449. * @param mixed $value Value to format
  450. * @param string $format Format code
  451. * @param array $callBack Callback function for additional formatting of string
  452. * @return string Formatted string
  453. */
  454. public static function toFormattedString($value = '', $format = '', $callBack = null)
  455. {
  456. // For now we do not treat strings although section 4 of a format code affects strings
  457. if (!is_numeric($value)) return $value;
  458. // For 'General' format code, we just pass the value although this is not entirely the way Excel does it,
  459. // it seems to round numbers to a total of 10 digits.
  460. if (($format === PHPExcel_Style_NumberFormat::FORMAT_GENERAL) || ($format === PHPExcel_Style_NumberFormat::FORMAT_TEXT)) {
  461. return $value;
  462. }
  463. // Get the sections, there can be up to four sections
  464. $sections = explode(';', $format);
  465. // Fetch the relevant section depending on whether number is positive, negative, or zero?
  466. // Text not supported yet.
  467. // Here is how the sections apply to various values in Excel:
  468. // 1 section: [POSITIVE/NEGATIVE/ZERO/TEXT]
  469. // 2 sections: [POSITIVE/ZERO/TEXT] [NEGATIVE]
  470. // 3 sections: [POSITIVE/TEXT] [NEGATIVE] [ZERO]
  471. // 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
  472. switch (count($sections)) {
  473. case 1:
  474. $format = $sections[0];
  475. break;
  476. case 2:
  477. $format = ($value >= 0) ? $sections[0] : $sections[1];
  478. $value = abs($value); // Use the absolute value
  479. break;
  480. case 3:
  481. $format = ($value > 0) ?
  482. $sections[0] : ( ($value < 0) ?
  483. $sections[1] : $sections[2]);
  484. $value = abs($value); // Use the absolute value
  485. break;
  486. case 4:
  487. $format = ($value > 0) ?
  488. $sections[0] : ( ($value < 0) ?
  489. $sections[1] : $sections[2]);
  490. $value = abs($value); // Use the absolute value
  491. break;
  492. default:
  493. // something is wrong, just use first section
  494. $format = $sections[0];
  495. break;
  496. }
  497. // Save format with color information for later use below
  498. $formatColor = $format;
  499. // Strip color information
  500. $color_regex = '/^\\[[a-zA-Z]+\\]/';
  501. $format = preg_replace($color_regex, '', $format);
  502. // Let's begin inspecting the format and converting the value to a formatted string
  503. if (preg_match('/^(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy]/i', $format)) { // datetime format
  504. // dvc: convert Excel formats to PHP date formats
  505. // strip off first part containing e.g. [$-F800] or [$USD-409]
  506. // general syntax: [$<Currency string>-<language info>]
  507. // language info is in hexadecimal
  508. $format = preg_replace('/^(\[\$[A-Z]*-[0-9A-F]*\])/i', '', $format);
  509. // OpenOffice.org uses upper-case number formats, e.g. 'YYYY', convert to lower-case
  510. $format = strtolower($format);
  511. $format = strtr($format,self::$_dateFormatReplacements);
  512. if (!strpos($format,'A')) { // 24-hour time format
  513. $format = strtr($format,self::$_dateFormatReplacements24);
  514. } else { // 12-hour time format
  515. $format = strtr($format,self::$_dateFormatReplacements12);
  516. }
  517. $dateObj = PHPExcel_Shared_Date::ExcelToPHPObject($value);
  518. $value = $dateObj->format($format);
  519. } else if (preg_match('/%$/', $format)) { // % number format
  520. if ($format === self::FORMAT_PERCENTAGE) {
  521. $value = round( (100 * $value), 0) . '%';
  522. } else {
  523. if (preg_match('/\.[#0]+/i', $format, $m)) {
  524. $s = substr($m[0], 0, 1) . (strlen($m[0]) - 1);
  525. $format = str_replace($m[0], $s, $format);
  526. }
  527. if (preg_match('/^[#0]+/', $format, $m)) {
  528. $format = str_replace($m[0], strlen($m[0]), $format);
  529. }
  530. $format = '%' . str_replace('%', 'f%%', $format);
  531. $value = sprintf($format, 100 * $value);
  532. }
  533. } else {
  534. if ($format === self::FORMAT_CURRENCY_EUR_SIMPLE) {
  535. $value = 'EUR ' . sprintf('%1.2f', $value);
  536. } else {
  537. // In Excel formats, "_" is used to add spacing, which we can't do in HTML
  538. $format = preg_replace('/_./', '', $format);
  539. // Some non-number characters are escaped with \, which we don't need
  540. $format = preg_replace("/\\\\/", '', $format);
  541. // Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
  542. $format = str_replace(array('"','*'), '', $format);
  543. // Find out if we need thousands separator
  544. // This is indicated by a comma enclosed by a digit placeholder:
  545. // #,# or 0,0
  546. $useThousands = preg_match('/(#,#|0,0)/', $format);
  547. if ($useThousands) {
  548. $format = preg_replace('/0,0/', '00', $format);
  549. $format = preg_replace('/#,#/', '##', $format);
  550. }
  551. // Scale thousands, millions,...
  552. // This is indicated by a number of commas after a digit placeholder:
  553. // #, or 0.0,,
  554. $scale = 1; // same as no scale
  555. $matches = array();
  556. if (preg_match('/(#|0)(,+)/', $format, $matches)) {
  557. $scale = pow(1000, strlen($matches[2]));
  558. // strip the commas
  559. $format = preg_replace('/0,+/', '0', $format);
  560. $format = preg_replace('/#,+/', '#', $format);
  561. }
  562. if (preg_match('/#?.*\?\/\?/', $format, $m)) {
  563. //echo 'Format mask is fractional '.$format.' <br />';
  564. if ($value != (int)$value) {
  565. $sign = ($value < 0) ? '-' : '';
  566. $integerPart = floor(abs($value));
  567. $decimalPart = trim(fmod(abs($value),1),'0.');
  568. $decimalLength = strlen($decimalPart);
  569. $decimalDivisor = pow(10,$decimalLength);
  570. $GCD = PHPExcel_Calculation_MathTrig::GCD($decimalPart,$decimalDivisor);
  571. $adjustedDecimalPart = $decimalPart/$GCD;
  572. $adjustedDecimalDivisor = $decimalDivisor/$GCD;
  573. if ((strpos($format,'0') !== false) || (strpos($format,'#') !== false) || (substr($format,0,3) == '? ?')) {
  574. if ($integerPart == 0) { $integerPart = ''; }
  575. $value = "$sign$integerPart $adjustedDecimalPart/$adjustedDecimalDivisor";
  576. } else {
  577. $adjustedDecimalPart += $integerPart * $adjustedDecimalDivisor;
  578. $value = "$sign$adjustedDecimalPart/$adjustedDecimalDivisor";
  579. }
  580. }
  581. } else {
  582. // Handle the number itself
  583. // scale number
  584. $value = $value / $scale;
  585. // Strip #
  586. $format = preg_replace('/\\#/', '', $format);
  587. $n = "/\[[^\]]+\]/";
  588. $m = preg_replace($n, '', $format);
  589. $number_regex = "/(0+)(\.?)(0*)/";
  590. if (preg_match($number_regex, $m, $matches)) {
  591. $left = $matches[1];
  592. $dec = $matches[2];
  593. $right = $matches[3];
  594. // minimun width of formatted number (including dot)
  595. $minWidth = strlen($left) + strlen($dec) + strlen($right);
  596. if ($useThousands) {
  597. $value = number_format(
  598. $value
  599. , strlen($right)
  600. , PHPExcel_Shared_String::getDecimalSeparator()
  601. , PHPExcel_Shared_String::getThousandsSeparator()
  602. );
  603. } else {
  604. $sprintf_pattern = "%0$minWidth." . strlen($right) . "f";
  605. $value = sprintf($sprintf_pattern, $value);
  606. }
  607. $value = preg_replace($number_regex, $value, $format);
  608. }
  609. }
  610. if (preg_match('/\[\$(.*)\]/u', $format, $m)) {
  611. // Currency or Accounting
  612. $currencyFormat = $m[0];
  613. $currencyCode = $m[1];
  614. list($currencyCode) = explode('-',$currencyCode);
  615. if ($currencyCode == '') {
  616. $currencyCode = PHPExcel_Shared_String::getCurrencyCode();
  617. }
  618. $value = preg_replace('/\[\$([^\]]*)\]/u',$currencyCode,$value);
  619. }
  620. }
  621. }
  622. // Additional formatting provided by callback function
  623. if ($callBack !== null) {
  624. list($writerInstance, $function) = $callBack;
  625. $value = $writerInstance->$function($value, $formatColor);
  626. }
  627. return $value;
  628. }
  629. }