rails decimal keep three position after point
Rails Tip: Precision and scale for decimals
For when you need that little bit of extra accuracy, specifying precision and scale for a decimal column in your Ruby on Rails migration is pretty simple. The precision represents the total number of digits in the number, whereas scale represents the number of digits following the decimal point. To specify the precision and scale, simply pass those as options to your column definition.
For example:
class AddLatLngToAddresses < ActiveRecord::Migration def self.up add_column :addresses, :lat, :decimal, :precision => 15, :scale => 10 add_column :addresses, :lng, :decimal, :precision => 15, :scale => 10 end def self.down remove_column :addresses, :lat remove_column :addresses, :lng end end
This will allow you to have 10 digits after the decimal point and 15 digits max.
One thing to note, however is that Rails will use BigDecimal as the type for the column. BigDecimal provides support for very large or very accurate floating point numbers. Remember those pesky floating point imprecision errors?
>> 1.2 - 1.0 == 0.2 => false
Yep, BigDecimal handles that…
>> BigDecimal.new('1.2') - BigDecimal.new('1.0') == BigDecimal.new('0.2') => true
So now, go forth and be accurate.
Also see
ActiveRecord::ConnectionAdapters::TableDefinition.column BigDecimal Latitude and Longitude in Rails
More of my rantings Installing aspell and raspell for Ultrasphinx Quick Tip: Route Associations Quick Tip: Override Rails Generated URLs These might also interest you Outsourcing Killed By Django And Ruby On Rails (Aware Labs) How To Prepare For Ignite (Preston Lee) Outsourcing Revisited And No One Is Safe (Aware Labs)
Posted April 12th, 2008 at 3:52 am in Ruby on Rails | Permalink
Login
Follow the discussion
Comment ( 1 )
Sort by: Date Rating Last Activity
0
Emerson · 40 weeks ago
Relatively new to Rails and never took the time to understand this simple idea. I just keep Googling it until today, when I realized it would be much better if I just took a few seconds to understand what 'precision' and 'scale' actually did.
Thanks for the clear explanation, quick and easy...
Reply
Post a new comment
Login to IntenseDebate Login to WordPress.com Login to Twitter Login to OpenID
Name
Website (optional)
Displayed next to your comments.
Not displayed publicly.
If you have a website, link to it here.
Submit Comment
Subscribe to None Replies All new comments
Comments by IntenseDebate
Subscribe
I'm a pragmatist, an idea generator and a doer of deeds that need doing. I may have an unhealthy obsession with pirates and coffee. I also enjoy making work for other people. Get to know me and you'll see what I mean.
Millarian
Musings of a startup junkie and Ruby on Rails nerd. Home About Resume Phoenix Startups Phoenix Entrepreneurs
Millarian is proudly powered by WordPress , and uses the Headless theme by Ozan Onay .
Avatar photo provided courtesy of the amazing Tyson Crosbie .
You can subscribe to an RSS feed of entries or comments .
查看更多关于rails decimal keep three position after point的详细内容...