Version: current

     1 <?php
     2 /**
     3  * Represents a decimal field from 0-1 containing a percentage value.
     4  * @package sapphire
     5  * @subpackage model
     6  */
     7 class Percentage extends Decimal {
     8
     9     /**
    10      * Create a new Decimal field.
    11      */
    12     function __construct($name, $precision = 4) {
    13         if(!$precision) $precision = 4;
    14
    15         parent::__construct($name, $precision + 1, $precision);
    16     }
    17
    18     function Nice() {
    19         return number_format($this->value * 100, $this->decimalSize - 2) . '%';
    20     }
    21
    22     function saveInto($dataObject) {
    23         parent::saveInto($dataObject);
    24
    25         $fieldName = $this->name;
    26         if($fieldName && $dataObject->$fieldName > 1.0) {
    27             $dataObject->$fieldName = 1.0;
    28         }
    29     }
    30 }
    31
    32 ?>
    33

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -