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 => ',') %>
inserting commas to separate large number?
Use number_with_delimiter.
From your helpers:
number_with_delimiter(12345678) # => 12,345,678
UPDATE: The code for the method.
Rails strip all except numbers commas and decimal points
Try:
rate = rate.gsub(/[^0-9,\.]/, '')
Basically, you know the ^
means not when inside the character class brackets [] which you are using, and then you can just add the comma to the list. The decimal needs to be escaped with a backslash because in regular expressions they are a special character that means "match anything".
Also, be aware of whether you are using gsub
or gsub!
gsub!
has the bang, so it edits the instance of the string you're passing in, rather than returning another one.
So if using gsub!
it would be:
rate.gsub!(/[^0-9,\.]/, '')
And rate
would be altered.
If you do not want to alter the original variable, then you can use the version without the bang (and assign it to a different var):
cleaned_rate = rate.gsub!(/[^0-9,\.]/, '')
I'd just google for tutorials. I haven't used one. Regexes are a LOT of time and trial and error (and table-flipping).
This is a cool tool to use with a mini cheat-sheet on it for ruby that allows you to quickly edit and test your expression:
http://rubular.com/
Chaining statements together with commas
It's not actually chaining statements, it's parallel assignment. It means: assign the value of b
to a
, and assign the value of a%b
to b
, at the same time.
Because the values are interdependent, it can't simply be written as:
# WRONG
a = b
b = a%b
You'd need a temporary variable:
# Correct, but more verbose and less readable
old_b = b
b = a%b
a = old_b
The same syntax is also useful when destructuring arrays, for example:
my_array = [1,2,3]
a, b, c = my_array
How to format a number 1000 as 1 000
see: http://www.justskins.com/forums/format-number-with-comma-37369.html
there is no built in way to it ( unless you using Rails, ActiveSupport Does have methods to do this) but you can use a Regex like
formatted_n = n.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
Allowing commas in a numerical field
I actually figured this out just as I was reading over my question.
The key is to use a virtual attribute.
class Job < ActiveRecord::Base
def flexible_quantity
quantity
end
def flexible_quantity=(quantity)
self.quantity = quantity.gsub(',', '') unless quantity.blank?
end
end
Then in the form, use the virtual attribute.
<%= f.text_field :flexible_quantity %>
Now validate the presence of the flexible_quantity value instead.
class Job < ActiveRecord::Base
validates :flexible_quantity, presence: true
# The cool this is that we can leave the numericality validation on the quantity field.
validates :quantity, presence: true, numericality: { only_integer: true }
end
Related Topics
Serving Static Files With Sinatra
Pass a Variable into a Partial, Rails 3
Haml: Append Class If Condition Is True in Haml
How to Access Method Arguments in Ruby
Installing Nokogiri on Osx 10.10 Yosemite
How to Create a Class Instance from a String Name in Ruby
In Ruby, How Does Coerce() Actually Work
Using Rails Migration on Different Database Than Standard "Production" or "Development"
Why Do I Get a "Permission Denied" Error While Installing a Gem
How to Search a Folder and All of Its Subfolders For Files of a Certain Type
Automatic Counter in Ruby For Each
Calculating the Number of Weeks in a Year with Ruby
Using Layouts in Haml Files Independently of Rails
Split the String to Get Only the First 5 Characters