I''ve got some basic code working for creating custom Numeric formats,
which can be used with a Numeric#to_format(:format) method (similar to
Time#to_s(:format).)
Here''s an example -- some custom formats, which can be added to the
environment:
Numeric::FORMATS = {
:dollars => {:precision => 2, :delimited => true, :unit_prefix =>
''$''},
:cents_to_dollars => lambda { |cents| (cents /
100.0).to_format(:dollars) },
:fluid_ounces => {:precision => 1, :delimited => true, :unit_suffix
=> '' fl oz''},
:sku => {:prefix => ''['', :unit_prefix =>
''SKU'', :suffix => '']''}
}
And their use:
2101.50.to_format(:dollars) # => "$2,101.50"
-2101.50.to_format(:dollars) # => "-$2,101.50"
210150.to_format(:cents_to_dollars) # => "$2,101.50"
16.to_format(:fluid_ounces) # => "16.0 fl oz"
912354678.to_format(:sku) # => [SKU912354678]
This allows measurements to be outputted with their units in a
consistent format across the app (as opposed to just in ActionView,
with the current numeric helpers.)
Would there be any interest in including something like this in the
core?
Thanks,
Geoff
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com
To unsubscribe from this group, send email to
rubyonrails-core-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---