Difference between #6 and #8 of
Custom Number Formatting or Decimal Separators and i18n

Changes

Title unchanged

Custom Number Formatting or Decimal Separators and i18n

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

decimal separator, number format, i18n, customize, floating point numbers, localization

Content changed

By default, the decimal separator in php (also in mysql) is a dot (.). So when we work with floats in Yii (in calculations, validation, sql statements etc.), the decimal separator has to be a dot. If we want to use for example a comma (,) as the decimal separator in our application, that is if we want to display numbers and enable users to enter numbers with a comma before the decimals, we have to:...

1. format the float value before printing/echoing : handle the output : 4.45 => "4,45"
[...]
```php
$product->attributes = $_POST['Product']; // or ...
$product->setAttribute('price', $_POST['Product']['price']); // or ...
 
$product->setAttributes(array('price'=>$_POST['Product']['price']));
```
`$product->price` will be a valid numerical value and available for further use.
[...]
###2. The 'unformatting' of a number
Extend the CActiveRecord class and override its function
s `setAttribute()` and `setAttributes()` to add the 'unformatting' functionality. <br>
For this, create the new file `components/ActiveRecord.php` with the following content:
[...]
}
}

 
  public function setAttribute($name,$value) {
 
    $column = $this->getTableSchema()->getColumn($name); // new
 
    if (stripos($column->dbType, 'decimal') !== false) // new
 
      $value = Yii::app()->format->unformatNumber($value); // new
 
    if(property_exists($this,$name))
 
      $this->$name=$value;
 
    else if(isset($this->getMetaData()->columns[$name]))
 
      $this->_attributes[$name]=$value;
 
    else
 
      return false;
 
    return true;
 
  }
 
}
```
[...]
```


 
Related Links
 
-------------
 
 
- Wiki: [How to handle decimal separators (e.g comma instead of dot) for l18n](http://www.yiiframework.com/wiki/298/how-to-handle-decimal-separators-e-g-comma-instead-of-dot-for-l18n "How to handle decimal separators (e.g comma instead of dot) for l18n")
 
- Forum: [Handling decimal separators](http://www.yiiframework.com/forum/index.php?/topic/28048-handling-decimal-separators-wiki-article/ "Handling decimal separators")
 
- Extension: [decimali18nbehavior](http://www.yiiframework.com/extension/decimali18nbehavior "decimali18nbehavior")
 
- Wiki: [How to extend CFormatter, add i18n support to booleanFormat and use it in CDetailView](http://www.yiiframework.com/wiki/305/how-to-extend-cformatter-add-i18n-support-to-booleanformat-and-use-it-in-cdetailview/ "How to extend CFormatter, add i18n support to booleanFormat and use it in CDetailView")
 
 
That's it. 
If something is unclear, wrong or incomplete, please let me know. Any suggestions for improvements will be appreciated!
4 0
16 followers
Viewed: 87 913 times
Version: 1.1
Category: Tutorials
Written by: c@cba
Last updated by: c@cba
Created on: Aug 1, 2012
Last updated: 12 years ago
Update Article

Revisions

View all history