PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/PHPExcel/Style/NumberFormat.php

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