I need to format float like price for two decimal places and thousands spaces, like this:
"1 082 233.00"
Use number_to_currency or number_with_precision:
number_with_precision(1082233, :precision => 2,
:separator => '.',
:delimiter => ' ')
# => '1 082 233.00'
See the NumberHelper documentation for details.
Related
My code
<td><%= number_to_currency(x['quote']['USD']['price'], :unit => '$', :separator =>'.', :delimiter => ',', precision: 4) %><br/></td>
When I use it just like that, it is ok, extract the info and put it ok. But when I try to multiply with another variable, it send me an error "ActiveSupport::SafeBuffer can't be coerced into BigDecimal", so I was thinking in turn it into a #, but I am not sure how to do it
You could think number_to_currency returns a special string (e.g. "$ 123.0000"), it can't be multiplied with number directly.
You could multiply first, then use number_to_currency to format result:
<% result = x['quote']['USD']['price'] * crypto.cost_per %>
<td><%= number_to_currency(result, unit: '$', separator: '.', delimiter: ',', precision: 4) %></td>
Why this code is not work?
<%= truncate(post.text, :length => post.text.rindex(".", 500)) %>
I need to trim the text on last dot before 500th symbol.
Use the :separator option. It will truncate at the dot before 500 characters
truncate(post.text, :length => 500, :separator => '.')
How would I parse my start_at column to be three different fields for hours, minutes and seconds instead of just one?
Note: I want to keep it as one column and not make three different ones.
Here is my code:
Table:
class CreateTimers < ActiveRecord::Migration
def change
create_table :timers do |t|
t.time :start_at
t.timestamps
end
end
end
Form:
<%= form_for(#timer) do |f| %>
<div class="form-inputs">
<%= f.text_field :start_at %>
</div>
<div class="form-actions">
<%= f.submit 'Start', :class => 'btn-primary span3' %>
</div>
<% end %>
If you just want to split time to three separate inputs you can use the time_select helper.
Otherwise use the strftime method; Check http://strfti.me for more help.
You don't say what format your time values are in. Because users love to be different, if you give them a free-form text field, odds are really good you'll get data in varying formats, so you'll have to be flexible.
You can try using DateTime's parse method, which provides some flexibility for the formats.
DateTime.parse('2001-02-03T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('20010203T040506+0700')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('3rd Feb 2001 04:05:06 PM')
#=> #<DateTime: 2001-02-03T16:05:06+00:00 ...>
There's also the Chronic gem, which allows even more flexibility in how the values can be entered.
Once you have a DateTime or Time value, you can use that object's methods to get at the hour, minute and second fields:
now = Time.now # => 2012-10-03 07:50:26 -0700
now.hour # => 7
now.min # => 50
now.sec # => 26
or:
require 'date'
now = DateTime.now # => #<DateTime: 2012-10-03T07:52:53-07:00 ((2456204j,53573s,622304000n),-25200s,2299161j)>
now.hour # => 7
now.min # => 52
now.sec # => 53
Check out strftime at http://www.ruby-doc.org/core-1.9.3/Time.html
For e.g.
start_at - 2011-04-26 04:14:56 UTC
For hours:
start_at.strftime("%H") = "04"
For minutes:
start_at.strftime("%M") = "14"
For seconds:
start_at.strftime("%S") = "26"
start_at.strftime("%H %M %S")
More detailed documentation is at: http://www.ruby-doc.org/core-1.9.3/Time.html
<%= message.content %>
I can display a message like this, but in some situations I would like to display only the first 5 words of the string and then show an ellipsis (...)
In rails 4.2 you can use truncate_words.
'Once upon a time in a world far far away'.truncate_words(4)
=> "Once upon a time..."
you can use truncate to limit length of string
truncate("Once upon a time in a world far far away", :length => 17, :separator => ' ')
# => "Once upon a..."
with given space separator it won't cut your words.
If you want exactly 5 words you can do something like this
class String
def words_limit(limit)
string_arr = self.split(' ')
string_arr.count > limit ? "#{string_arr[0..(limit-1)].join(' ')}..." : self
end
end
text = "aa bb cc dd ee ff"
p text.words_limit(3)
# => aa bb cc...
Try the following:
'this is a line of some words'.split[0..3].join(' ')
=> "this is a line"
# Message helper
def content_excerpt(c)
return unlessc
c.split(" ")[0..4].join + "..."
end
# View
<%= message.content_excerpt %>
But the common way is truncate method
# Message helper
def content_excerpt(c)
return unless c
truncate(c, :length => 20)
end
I need to show user amount presented in different currencies. e.q. :
Your balance: $ 100 000.00
€ 70 000.00
3 000 000,00 руб.
So I need to use number_to_currency three times with different locales(en, eu, ru). What is the right way to do it?
I don't think you actually need different locales, because you have just balances in different currencies. You can simply pass additional arguments to number_to_currency. Something like this:
number_to_currency(70000.00, :unit => "€", :separator => ".", :delimiter => " ", :format => "%u %n")
This will display: € 70 000.00
Additionally it seems that you can set :locale option when calling number_to_currency. It's not documented, but here is the part of the number_to_currency code:
defaults = I18n.translate('number.format''number.format', :locale => options[:locale], :raise => true) rescue {}
currency = I18n.translate('number.currency.format''number.currency.format', :locale => options[:locale], :raise => true) rescue {}
So you should be able to do something like:
number_to_currency(70000.00, :locale => :ru)