mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-09 11:11:21 +02:00
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112337 467b73ca-7a2a-4603-9d3b-597d59a354a9
59 lines
1.4 KiB
PHP
59 lines
1.4 KiB
PHP
<?php
|
|
/**
|
|
* Represents a decimal field containing a currency amount.
|
|
* Currency the currency class only supports single currencies.
|
|
*
|
|
* Example definition via {@link DataObject::$db}:
|
|
* <code>
|
|
* static $db = array(
|
|
* "Price" => "Currency",
|
|
* "Tax" => "Currency(5)",
|
|
* );
|
|
* </code>
|
|
*
|
|
* @deprecated 2.5 Use Money class
|
|
*
|
|
* @package sapphire
|
|
* @subpackage model
|
|
*/
|
|
class Currency extends Decimal {
|
|
protected static $currencySymbol = '$';
|
|
|
|
/**
|
|
* Returns the number as a currency, eg “$1,000.00”.
|
|
*/
|
|
function Nice() {
|
|
// return "<span title=\"$this->value\">$" . number_format($this->value, 2) . '</span>';
|
|
$val = self::$currencySymbol . number_format(abs($this->value), 2);
|
|
if($this->value < 0) return "($val)";
|
|
else return $val;
|
|
}
|
|
|
|
/**
|
|
* Returns the number as a whole-number currency, eg “$1,000”.
|
|
*/
|
|
function Whole() {
|
|
$val = self::$currencySymbol . number_format(abs($this->value), 0);
|
|
if($this->value < 0) return "($val)";
|
|
else return $val;
|
|
}
|
|
|
|
function setValue($value) {
|
|
$matches = null;
|
|
if(is_numeric($value)) {
|
|
$this->value = $value;
|
|
|
|
} else if(preg_match('/-?\$?[0-9,]+(.[0-9]+)?([Ee][0-9]+)?/', $value, $matches)) {
|
|
$this->value = str_replace(array('$',',',self::$currencySymbol),'',$matches[0]);
|
|
|
|
} else {
|
|
$this->value = 0;
|
|
}
|
|
}
|
|
|
|
static function setCurrencySymbol($value) {
|
|
self::$currencySymbol = $value;
|
|
}
|
|
}
|
|
|
|
?>
|