PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/add-ons/PHPExcel/PHPExcel/Style/NumberFormat.php

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