PageRenderTime 60ms CodeModel.GetById 13ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 1ms

/PHPExcel/Style/NumberFormat.php

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