1: <?php
2: /**
3: * This file contains the number datatype class.
4: *
5: * @package Core
6: * @subpackage Datatype
7: * @version SVN Revision $Rev:$
8: *
9: * @author unknown
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * Number datatype class.
20: *
21: * @package Core
22: * @subpackage Datatype
23: */
24: class cDatatypeNumber extends cDatatype {
25:
26: protected $_iPrecision;
27:
28: protected $_sThousandSeparatorCharacter;
29:
30: protected $_sDecimalPointCharacter;
31:
32: public function __construct() {
33: $language = cI18n::getLanguage();
34:
35: // Try to find out the current locale settings
36: $aLocaleSettings = cLocaleConv($language);
37:
38: $this->setDecimalPointCharacter($aLocaleSettings["mon_decimal_point"]);
39: $this->setThousandSeparatorCharacter($aLocaleSettings["mon_thousands_sep"]);
40:
41: parent::__construct();
42: }
43:
44: public function set($value) {
45: $this->_mValue = floatval($value);
46: }
47:
48: public function get() {
49: return $this->_mValue;
50: }
51:
52: public function setPrecision($iPrecision) {
53: $this->_iPrecision = $iPrecision;
54: }
55:
56: public function setDecimalPointCharacter($sCharacter) {
57: $this->_sDecimalPointCharacter = $sCharacter;
58: }
59:
60: public function getDecimalPointCharacter() {
61: return ($this->_sDecimalPointCharacter);
62: }
63:
64: public function setThousandSeparatorCharacter($sCharacter) {
65: $this->_sThousandSeparatorCharacter = $sCharacter;
66: }
67:
68: public function getThousandSeparatorCharacter() {
69: return ($this->_sThousandSeparatorCharacter);
70: }
71:
72: /**
73: *
74: * @throws cException if the decimal separator character and the thousand
75: * separator character are equal
76: */
77: public function parse($value) {
78: if ($this->_sDecimalPointCharacter == $this->_sThousandSeparatorCharacter) {
79: throw new cException("Decimal point character cannot be the same as the thousand separator character. Current decimal point character is '{$this->_sDecimalPointCharacter}', current thousand separator character is '{$this->_sThousandSeparatorCharacter}'");
80: }
81:
82: // Convert to standard english format
83: $value = str_replace($this->_sThousandSeparatorCharacter, "", $value);
84: $value = str_replace($this->_sDecimalPointCharacter, ".", $value);
85:
86: $this->_mValue = floatval($value);
87: }
88:
89: public function render() {
90: return number_format($this->_mValue, $this->_iPrecision, $this->_sDecimalPointCharacter, $this->_sThousandSeparatorCharacter);
91: }
92:
93: }
94:
95: ?>