How to use Rails number_to_currency options - ruby-on-rails

I'm using Rails and wanted to convert a float to currency. I'm using number_to_currency in a Sidekiq Worker, so I've added include ActionView::Helpers::NumberHelper in the file and where I want the conversion I've added the following:
number_to_currency(value, unit: '', delimeter: '.', separator: ',')
And I wanted the result something like: 1.200,09 but its not working. An example of values I have and want to convert is: 1001.4290000000004 which should be converted to 1.001,43. I've also tried:
number_to_currency(final_total, unit: '', delimeter: ',', separator: '.')
And still doesn't work. I'm getting is 1,200.09. I'm using Rails 4.2.x
What am I missing here?

If you want get result like this 1001.4290000000004 => 1,001.43, you can use this method:
number_with_delimiter(value , delimiter: ",", separator: ".")
But befor u need convert value to the desired value:
value = 1001.4290000000004.round(2) #=> 1001.43

You just have a typo in word delimiter. It should be as follows:
number_to_currency(value, unit: '', delimiter: '.', separator: ',')

Related

Using number_to_currency with user parameter

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>

How to replace Space with Line Break in Ruby on Rails?

I am trying to replace Space in a string with Line Break in Ruby on Rails,
name = 'john smith'
i have tried the following so far:
name.gsub!(" ", "\n")
name.gsub!(" ", "<br>")
name.sub(" ", "\n")
name.sub(" ", "<br>")
but none of the above worked.
You have to be careful when marking a string as html_safe, especially if it may contain user input:
name = 'john smith<script>alert("gotcha")</script>'
name.gsub(' ', '<br>').html_safe
#=> "john<br>smith<script>alert(\"gotcha\")</script>"
Rails would output that string as-is, i.e. including the <script> tag.
In order to take advantage of Rails' HTML escaping, you should only mark the trusted parts as being html_safe. For a manually concatenated string:
''.html_safe + 'john' + '<br>'.html_safe + 'smith<script>alert("gotcha")</script>'
#=> "john<br>smith<script>alert("gotcha")</script>"
As you can see, only the <br> tag was left intact, the remaining parts were properly escaped.
There are several helpers for building safe strings as well as for building HTML tags. In your case, I'd use safe_join and tag:
name = 'john smith<script>alert("gotcha")</script>'
safe_join(name.split(' '), tag(:br))
#=> "john<br />smith<script>alert("gotcha")</script>"
While printing it in html you will need to use raw, otherwise rails will escape the tags
= raw name.gsub(" ", "<br>")
Try another one:
<%= name.gsub(" ", "<br>").html_safe %>
html_safe :
Marks a string as trusted safe. It will be inserted into HTML with no additional escaping performed.
"<a>Hello</a>".html_safe
#=> "<a>Hello</a>"
nil.html_safe
#=> NoMethodError: undefined method `html_safe' for nil:NilClass
raw :
raw is just a wrapper around html_safe. Use raw if there are chances that the string will be nil.
raw("<a>Hello</a>")
#=> "<a>Hello</a>"
raw(nil)
#=> ""

Internationalization of floats to display a , instead of

Rails display floating numbers with a . : 5.86 using the american way of displaying numbers. How can i make it display them the european way : 5,86 ?
You can use the number_with_precision helper method from the ActionView::Helpers::NumberHelper module:
number_with_precision(5.86, precision: 2, separator: ',') # => 5,86

Truncate & Simple Format String in Ruby on Rails

I am trying to take a string and render it with simple_format while at the same time truncating it. It works when I just use one or the other, but not when I do both. Why is this not doing simple_format and truncating simultaneously.
Controller
myString = "Apple’s New Laptop"
View
<%= simple_format truncate( myString, :length => 20 ) %>
The way to do this is to truncate after you have used simple_format on the string. Since truncate escapes the string by default, you have to use the escape: false option.
> myString = "Apple’s New Laptop"
> truncate(simple_format(myString), escape: false)
> => "<p>Apple’s New Laptop..."
> truncate(simple_format(myString), escape: false, length: 19)
> => "<p>Apple’s..."
This has the potential to create unbalanced HTML tags by cutting the </p> for example, so use carefully.
There was something changed in the truncate-helper in Rails 4.
The documentation does tells us:
The result is marked as HTML-safe, but it is escaped by default, unless :escape is false.
http://apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/truncate
Rails normally escapes all strings. If you just want to put some unicode chars in strings in your code, you can do it by using the \u notation with the hexadecimal code. Then truncate will also count the char as exactly one char.
myString = "Apple\u2019s New Laptop"
This might be late but useful to someone else. This worked for me.
<%= truncate(myString, escape: false, length: 20 ) %>

Ruby on Rails number_to_currency how to do without Unit [without symbol, $ or other]

How to format my number with the helper number_to_currency, but show it without the currency symbol ($, R$, etc)?
Ex.:
$ 500,78 -> Not what i want
500,78 -> Format i desire
Try using it with the :unit option set to empty string.
number_to_currency(1234567890.50, :unit => "")

Resources