Does Ruby have any number formatting classes?
Ruby has all the standard print formatters, available either via printf, sprintf or using 'formatstring' % [var1, ...]
.
>> '%.2f' % 3.14159 #=> "3.14"
>> '%4s %-4s' % ['foo', 'bar'] #=> " foo bar "
Verify formatting in a number
s = '74,456,890'
if s =~ /^\d{1,3}(?:,\d{3}+)$/
# String s has the required format
else
# String s does not have the required format
end
number format in rails form
<%= f.text_field :amount, :class => 'text_field', :value => f.object.amount.to_i %>
Add class for number_with_precision
You have to split number.value with '.' as separator, and use it like
full_value = number.value.to_s.split('.')
haml: %b #{full_value[0]}.
%span #{full_value[1]}
Rails: Is there a rails trick to adding commas to large numbers?
You want the number_with_delimiter
method. For example:
<%= number_with_delimiter(@number, :delimiter => ',') %>
Alternatively, you can use the number_with_precision
method to ensure that the number is always displayed with two decimal places of precision:
<%= number_with_precision(@number, :precision => 2, :delimiter => ',') %>
Formatting a number to split at every third digit
>> def spaces_on number
>> number.to_s.gsub(/\D/, '').reverse.gsub(/.{3}/, '\0 ').reverse
>> end
=> nil
>> spaces_on 12345678
=> "12 345 678"
Maybe there's an argument that regular expressions aren't amazingly readable, but personally I think they're simpler to understand than having to think in recursion.
Saving a number with specific decimal places to a variable
The method number_with_precision
is a helper method for views only, and it returns a string version of the number. It doesn't return a number with the specified precision, if that's what you're looking for. The good news is, if you want exactly what number_with_precision
gives you, you can use sprintf
.
sprintf('%.2f', 3.1415926) # result: "3.14"
sprintf
returns a value you can store, instead of printing to stdout like printf
does. So you can easily capture it:
number = sprintf('%.2f', 3.1415926)
If you wanted to treat it as a number, you could probably build a class that does this for you, and converts to and from a Float as needed.
Is there a gem that normalizes and format US phone numbers in ruby?
Earlier this year, I reviewed a bunch of ruby gems that parse and format phone numbers. They fall into a number of groups (see below). TLDR: I used 'phone'. It might work for you because you can specify a default country code that it uses if your phone number doesn't include one.
1) US-centric:
big-phoney (0.1.4)
phone_wrangler (0.1.3)
simple_phone_number (0.1.9)
2) depends on rails or active_record:
phone_number (1.2.0)
validates_and_formats_phones (0.0.7)
3) forks of 'phone' that have been merged back into the trunk:
elskwid-phone (0.9.9.4)
tfe-phone (0.9.9.1)
4) relies on you to know the region ahead of time
phoney (0.1.0)
5) Kind of almost works for me
phone (0.9.9.3)
6) does not contain the substring 'phone' in the gem name (edit: I see you tried this one)
phony (1.6.1)
These groupings may be somewhat unfair or out of date so feel free to comment. I must admit I was a little frustrated at the time at how many people had partially re-invented this particular wheel.
Related Topics
Difference Between or and || When Setting Variables
Missing File in Gem After Build
Undefined Method (Nomethoderror) Ruby
How to Batch Convert Mp4 Files to Ogg with Ffmpeg Using a Bash Command or Ruby
Removing or Overriding an Activerecord Validation Added by a Superclass or Mixin
Ruby/Pgsql Error on Rails Start:Cannot Load Such File -- Pg_Ext (Loaderror)
Why Is Counting Letters Faster Using String#Count Than Using String#Chars in Ruby
Gem Install Cannot Find a Header File
How to Save Heroku Logs to Text File
How to Read Files in an Eventmachine-Based App
How to Model a Mutual Friendship in Rails
How to Print Stdout Immediately
Mismatched Bundler Version - Bundler 2, Ruby 2.6
What Order Do Before Filters Occur In