Rails5: How to format currency values from API data

ruby currency country
number to currency rails
ruby cents to dollars
ruby convert money to float
ruby number format
multi currency rails
rubymoney
ruby money eu_central_bank

I have a Ruby on Rails application that successfully requested data from an API but want it to be in the standard format of 4,992.02 instead of 4,992.0293

<div class="card-body">
  <h1 class="card-title pricing-card-title"><i class="fa fa-gbp" aria-hidden="true"></i><%= @bitcoin ["bpi"]["GBP"]["rate"] %></h1>
</div>

so the [Rate] is where the number is coming from. Used the standard number_to_currency method

<%= @bitcoin ["bpi"]["GBP"] number_to_currency(["rate"]) %> 

but get error

updated with error <%= @bitcoin ["bpi"]["GBP"] number_to_currency(["rate"]) %>

/Users/neilpatel/Desktop/2018/rails/bit/app/views/home/index.html.erb:13: syntax error, unexpected tIDENTIFIER, expecting ')' ...bpi"]["GBP"] number_to_currency(["rate"]) );@output_buffer.s... ... ^

Use cant use round, sting contains , comma.

// helpers/app_helper.rb
module ApplicationHelper
   def coin_to_currency(number)
           n = number.split(".")
           num = n[0]
           dec = n[1].slice(0..1)
           number_to_currency([num, dec].join("."))
   end
end


// view
<%= coin_to_currency( PLACE_COIN_NUMBER_HERE ) %> 

I have a Ruby on Rails application that successfully requested data from an API but want it to be in the standard format of 4,992.02 instead of  This is result from your input '4,992.0293' and it is rounded up to 2 decimal places because 0293 if you cut to 2 numbers then corrected value is 03.


So I suggest you this solution for your request:

This lines in your controller:

@split_result = '4,992.0293'.split(',')
@result = @split_result[0] + @split_result[1].to_f.round(2).to_s

This line goes to your view:

<%=  number_with_delimiter(@result) %>

This is result from your input '4,992.0293' and it is rounded up to 2 decimal places because 0293 if you cut to 2 numbers then corrected value is 03.

A Ruby Library for dealing with money and currency conversion. If you miss String parsing, check out the new monetize gem. subunit_to_unit for HUF currency Opened by svilensiderov about 2 months ago #927 MGA currency Formatting  Formats a number into a currency string (e.g., $13.65). You can customize the format in the options hash. The currency unit and number formatting of the current locale will be used unless otherwise specified in the provided options. No currency conversion is performed.


try this

"4,9920.0293"[/[\d+,?\d+]+\.?\d{0,2}/]
 => "4,9920.02"

The currency unit and number formatting of the current locale will be used also be able to change the relative value of the currency displayed with this helper. Returns an expression formatted as a currency value using the currency symbol defined in the system control panel. public static string FormatCurrency (object Expression, int NumDigitsAfterDecimal = -1, Microsoft.VisualBasic.TriState IncludeLeadingDigit = Microsoft.VisualBasic.TriState.UseDefault, Microsoft.VisualBasic.TriState


A Ruby Library for dealing with money and currency conversion. all information about a certain amount of money, such as its value and its currency. Formatting (see Formatting section for more options) Money.new(100, "USD").format Other attributes, such as :name or :priority , exist to provide a basic API you can take  The default display value for numeric values is the “general” ( g ) format with pd.options.display.precision precision. Examples. >>> df = pd.DataFrame(np.random.randn(4, 2), columns=['a', 'b']) >>> df.style.format("{:.2%}") >>> df['c'] = ['a', 'b', 'c', 'd'] >>> df.style.format( {'c': str.upper}) pandas.io.formats.style.Styler.export pandas.io.formats.style.Styler.from_custom_template.


The Rails 5 attributes API is the best feature of Rails 5, but no one knows it yet. from user input (or any setter) and retrieved from the database (or any getter). Converting phone number fields to e-164 format using a library like You should inherit from ActiveRecord::Type::Value or a more specific type,  Select the cells you want to format. On the Home tab, click the Dialog Box Launcher next to Number. Tip: You can also press Ctrl+1 to open the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Currency or Accounting. In the Symbol box, click the currency symbol that you want.


In Ruby, you can easily format strings using % (short for Kernel#sprintf): It will limit the total number of displayed digits, disregarding the decimal point numbers such as prices or totals in an SQL database, always use a DECIMAL column. has not accessed or used the YouTube Data API Service in the past 60 days. Say you have a number like 10, and it represents the price of something. You want to transform it to $10,00. If the number has more than 3 digits however it should be displayed differently, for example 1000 should be displayed as $1,000.00. This is in USD, however. Different countries have different conventions to display values.