I have a database table field defined as decimal(10,5). I want to use it for various purposes so I don’t want to change the datatype. In most of the forms however I want to keep this field showing none or very limited number of decimal points. For example, In a form I would want to show it having 1,1.5,2,2.5… sequence only.
Here’s a simple yet useful solution.
[php]$this->request->data[‘Question’][‘defaultmark’] = number_format($this->request->data[‘Question’][‘defaultmark’], 1);
echo $this->Form->input(‘defaultmark’, array(‘label’=>__(‘Default mark’), ‘step’=>’0.5’));[/php]
First line in the example above formats the db value to 1 decimal point. This line of logic can be placed anywhere, perhaps in a controller action, model callback function or in a behavior as per your requirements and convenience. The second line of code steps the input field by 0.5 on cursor up/down movement.