# Form field --display :decimal 0.0 as 0.00, or blank?

**URL:** <https://www.ruby-forum.com/t/form-field-display-decimal-0-0-as-0-00-or-blank/117144>\
**Category:** Rails\
**Created:** [October 13, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/form-field-display-decimal-0-0-as-0-00-or-blank/117144 "2007-10-13T20:40:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@tharrison](https://www.ruby-forum.com/u/tharrison)\
**Post date:** [October 13, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/form-field-display-decimal-0-0-as-0-00-or-blank/117144/1 "2007-10-13T20:40:03Z")

</div>

Howdy –

I have a price field in my db, :decimal, :null =\> false, :default =\>  
0

When I open a new form field to create a new record, this value is  
displayed as “0.0”. This is not so pretty for a field in which a  
price like “123.45” is expected. Ideally I would like to have the  
field blank (I’ll validate for numeric at submit time). I’m aware of  
the format\_as\_currency helper, but I am not sure how/if I would use it  
in a form context.

Bonus question: I am currently validating acceptable characters in a  
javascript onkeyup event (get rid of anything not a digit, decimal or  
comma). Is there anything that comes for free that I have missed, or  
a good plugin out there to help with what I would think is a fairly  
common use case?

Obviously, I’m a rails noob 🙂

Regards, and thanks!

Tom

---

<div class="post-metadata">

**Author:** ![tharrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@tharrison](https://www.ruby-forum.com/u/tharrison)\
**Post date:** [October 13, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/form-field-display-decimal-0-0-as-0-00-or-blank/117144/2 "2007-10-13T22:28:11Z")

</div>

Well, then you need to remove :null =\> false and :default =\> 0 from  
migration.

and then just do the validation 🙂

---

<div class="post-metadata">

**Author:** ![tharrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@tharrison](https://www.ruby-forum.com/u/tharrison)\
**Post date:** [October 13, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/form-field-display-decimal-0-0-as-0-00-or-blank/117144/3 "2007-10-13T23:14:33Z")

</div>

On Oct 13, 4:28 pm, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Well, then you need to remove :null =\> false and :default =\> 0 from  
> migration.
> 
> ## and then just do the validation 🙂
> 
> Posted viahttp://www.ruby-forum.com/.

Thanks. Yes, your solution will fix the display, but…

The migration defines both the type of the object within the Rails  
context, but also the actual database field definition, right? If I  
want to have very basic data integrity, using not null, defaults (not  
to mention explicit foreign key constraints) is a “best practice”  
which I am pretty sure is still in force in the Rails world.

Any other ideas on how I can change the display of my data without  
changing the migration and database column definitions?

Thanks!!

Tom

---

<div class="post-metadata">

**Author:** ![tharrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9e8a1a/32.png) [@tharrison](https://www.ruby-forum.com/u/tharrison)\
**Post date:** [October 14, 2007, 5:17am UTC](https://www.ruby-forum.com/t/form-field-display-decimal-0-0-as-0-00-or-blank/117144/4 "2007-10-14T05:17:32Z")

</div>

> Any other ideas on how I can change the display of my data without  
> changing the migration and database column definitions?

you could try overriding the getter in your model:

assuming the field is ‘my\_number’:

def my\_number  
return nil if self[:my\_number].blank? || self[:my\_number] == 0  
self[:my\_number]  
end
