Version: current

     1 <?php
     2 /**
     3  * Represents a floating point field.
     4  * @package sapphire
     5  * @subpackage model
     6  */
     7 class Float extends DBField {
     8
     9     function __construct($name, $defaultVal = 0) {
    10         $this->defaultVal = is_float($defaultVal) ? $defaultVal : (float) 0;
    11
    12         parent::__construct($name);
    13     }
    14
    15     function requireField() {
    16         $parts=Array('datatype'=>'float', 'null'=>'not null', 'default'=>$this->defaultVal, 'arrayValue'=>$this->arrayValue);
    17         $values=Array('type'=>'float', 'parts'=>$parts);
    18         DB::requireField($this->tableName, $this->name, $values);
    19     }
    20
    21     function Nice() {
    22         return number_format($this->value, 2);
    23     }
    24
    25     function Round($precision = 3) {
    26         return round($this->value, $precision);
    27     }
    28
    29     function NiceRound($precision = 3) {
    30         return number_format(round($this->value, $precision), $precision);
    31     }
    32
    33     public function scaffoldFormField($title = null) {
    34         return new NumericField($this->name, $title);
    35     }
    36
    37     /**
    38      * Returns the value to be set in the database to blank this field.
    39      * Usually it's a choice between null, 0, and ''
    40      */
    41     function nullValue() {
    42         return 0;
    43     }
    44
    45     /**
    46      * Return an encoding of the given value suitable for inclusion in a SQL statement.
    47      * If necessary, this should include quotes.
    48      */
    49     function prepValueForDB($value) {
    50         if($value === true) {
    51             return 1;
    52         }
    53         if(!$value || !is_numeric($value)) {
    54             if(strpos($value, '[') === false) {
    55                 return '0';
    56             } else {
    57                 return Convert::raw2sql($value);
    58             }
    59         } else {
    60             return Convert::raw2sql($value);
    61         }
    62     }
    63
    64 }

[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. -