<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Module: ActionView::Helpers::NumberHelper</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Module</strong></td> <td class="class-name-in-header">ActionView::Helpers::NumberHelper</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../../files/lib/action_view/helpers/number_helper_rb.html"> lib/action_view/helpers/number_helper.rb </a> <br /> </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <p> Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, and file size. </p> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000446">number_to_currency</a> <a href="#M000450">number_to_human_size</a> <a href="#M000447">number_to_percentage</a> <a href="#M000445">number_to_phone</a> <a href="#M000448">number_with_delimiter</a> <a href="#M000449">number_with_precision</a> </div> </div> </div> <!-- if includes --> <div id="section"> <div id="constants-list"> <h3 class="section-bar">Constants</h3> <div class="name-list"> <table summary="Constants"> <tr class="top-aligned-row context-row"> <td class="context-item-name">STORAGE_UNITS</td> <td>=</td> <td class="context-item-value">[:byte, :kb, :mb, :gb, :tb].freeze</td> </tr> </table> </div> </div> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000446" class="method-detail"> <a name="M000446"></a> <div class="method-heading"> <a href="NumberHelper.src/M000446.html" target="Code" class="method-signature" onclick="popupCode('NumberHelper.src/M000446.html');return false;"> <span class="method-name">number_to_currency</span><span class="method-args">(number, options = {})</span> </a> </div> <div class="method-description"> <p> Formats a <tt>number</tt> into a currency string (e.g., $13.65). You can customize the format in the <tt>options</tt> hash. </p> <h4>Options</h4> <ul> <li><tt>:precision</tt> - Sets the level of precision (defaults to 2). </li> <li><tt>:unit</tt> - Sets the denomination of the currency (defaults to "$"). </li> <li><tt>:separator</tt> - Sets the separator between the units (defaults to "."). </li> <li><tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ","). </li> <li><tt>:format</tt> - Sets the format of the output string (defaults to "%u%n"). The field types are: <pre> %u The currency unit %n The number </pre> </li> </ul> <h4>Examples</h4> <pre> number_to_currency(1234567890.50) # => $1,234,567,890.50 number_to_currency(1234567890.506) # => $1,234,567,890.51 number_to_currency(1234567890.506, :precision => 3) # => $1,234,567,890.506 number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "") # => &pound;1234567890,50 number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "", :format => "%n %u") # => 1234567890,50 &pound; </pre> </div> </div> <div id="method-M000450" class="method-detail"> <a name="M000450"></a> <div class="method-heading"> <a href="NumberHelper.src/M000450.html" target="Code" class="method-signature" onclick="popupCode('NumberHelper.src/M000450.html');return false;"> <span class="method-name">number_to_human_size</span><span class="method-args">(number, *args)</span> </a> </div> <div class="method-description"> <p> Formats the bytes in <tt>size</tt> into a more understandable representation (e.g., giving it 1500 yields 1.5 KB). This method is useful for reporting file sizes to users. This method returns nil if <tt>size</tt> cannot be converted into a number. You can customize the format in the <tt>options</tt> hash. </p> <h4>Options</h4> <ul> <li><tt>:precision</tt> - Sets the level of precision (defaults to 1). </li> <li><tt>:separator</tt> - Sets the separator between the units (defaults to "."). </li> <li><tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ""). </li> </ul> <h4>Examples</h4> <pre> number_to_human_size(123) # => 123 Bytes number_to_human_size(1234) # => 1.2 KB number_to_human_size(12345) # => 12.1 KB number_to_human_size(1234567) # => 1.2 MB number_to_human_size(1234567890) # => 1.1 GB number_to_human_size(1234567890123) # => 1.1 TB number_to_human_size(1234567, :precision => 2) # => 1.18 MB number_to_human_size(483989, :precision => 0) # => 473 KB number_to_human_size(1234567, :precision => 2, :separator => ',') # => 1,18 MB </pre> <p> You can still use <tt><a href="NumberHelper.html#M000450">number_to_human_size</a></tt> with the old API that accepts the <tt>precision</tt> as its optional second parameter: </p> <pre> number_to_human_size(1234567, 2) # => 1.18 MB number_to_human_size(483989, 0) # => 473 KB </pre> </div> </div> <div id="method-M000447" class="method-detail"> <a name="M000447"></a> <div class="method-heading"> <a href="NumberHelper.src/M000447.html" target="Code" class="method-signature" onclick="popupCode('NumberHelper.src/M000447.html');return false;"> <span class="method-name">number_to_percentage</span><span class="method-args">(number, options = {})</span> </a> </div> <div class="method-description"> <p> Formats a <tt>number</tt> as a percentage string (e.g., 65%). You can customize the format in the <tt>options</tt> hash. </p> <h4>Options</h4> <ul> <li><tt>:precision</tt> - Sets the level of precision (defaults to 3). </li> <li><tt>:separator</tt> - Sets the separator between the units (defaults to "."). </li> <li><tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ""). </li> </ul> <h4>Examples</h4> <pre> number_to_percentage(100) # => 100.000% number_to_percentage(100, :precision => 0) # => 100% number_to_percentage(1000, :delimiter => '.', :separator => ',') # => 1.000,000% number_to_percentage(302.24398923423, :precision => 5) # => 302.24399% </pre> </div> </div> <div id="method-M000445" class="method-detail"> <a name="M000445"></a> <div class="method-heading"> <a href="NumberHelper.src/M000445.html" target="Code" class="method-signature" onclick="popupCode('NumberHelper.src/M000445.html');return false;"> <span class="method-name">number_to_phone</span><span class="method-args">(number, options = {})</span> </a> </div> <div class="method-description"> <p> Formats a <tt>number</tt> into a US phone number (e.g., (555) 123-9876). You can customize the format in the <tt>options</tt> hash. </p> <h4>Options</h4> <ul> <li><tt>:area_code</tt> - Adds parentheses around the area code. </li> <li><tt>:delimiter</tt> - Specifies the delimiter to use (defaults to "-"). </li> <li><tt>:extension</tt> - Specifies an extension to add to the end of the generated number. </li> <li><tt>:country_code</tt> - Sets the country code for the phone number. </li> </ul> <h4>Examples</h4> <pre> number_to_phone(5551234) # => 555-1234 number_to_phone(1235551234) # => 123-555-1234 number_to_phone(1235551234, :area_code => true) # => (123) 555-1234 number_to_phone(1235551234, :delimiter => " ") # => 123 555 1234 number_to_phone(1235551234, :area_code => true, :extension => 555) # => (123) 555-1234 x 555 number_to_phone(1235551234, :country_code => 1) # => +1-123-555-1234 number_to_phone(1235551234, :country_code => 1, :extension => 1343, :delimiter => ".") => +1.123.555.1234 x 1343 </pre> </div> </div> <div id="method-M000448" class="method-detail"> <a name="M000448"></a> <div class="method-heading"> <a href="NumberHelper.src/M000448.html" target="Code" class="method-signature" onclick="popupCode('NumberHelper.src/M000448.html');return false;"> <span class="method-name">number_with_delimiter</span><span class="method-args">(number, *args)</span> </a> </div> <div class="method-description"> <p> Formats a <tt>number</tt> with grouped thousands using <tt>delimiter</tt> (e.g., 12,324). You can customize the format in the <tt>options</tt> hash. </p> <h4>Options</h4> <ul> <li><tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ","). </li> <li><tt>:separator</tt> - Sets the separator between the units (defaults to "."). </li> </ul> <h4>Examples</h4> <pre> number_with_delimiter(12345678) # => 12,345,678 number_with_delimiter(12345678.05) # => 12,345,678.05 number_with_delimiter(12345678, :delimiter => ".") # => 12.345.678 number_with_delimiter(12345678, :separator => ",") # => 12,345,678 number_with_delimiter(98765432.98, :delimiter => " ", :separator => ",") # => 98 765 432,98 </pre> <p> You can still use <tt><a href="NumberHelper.html#M000448">number_with_delimiter</a></tt> with the old API that accepts the <tt>delimiter</tt> as its optional second and the <tt>separator</tt> as its optional third parameter: </p> <pre> number_with_delimiter(12345678, " ") # => 12 345.678 number_with_delimiter(12345678.05, ".", ",") # => 12.345.678,05 </pre> </div> </div> <div id="method-M000449" class="method-detail"> <a name="M000449"></a> <div class="method-heading"> <a href="NumberHelper.src/M000449.html" target="Code" class="method-signature" onclick="popupCode('NumberHelper.src/M000449.html');return false;"> <span class="method-name">number_with_precision</span><span class="method-args">(number, *args)</span> </a> </div> <div class="method-description"> <p> Formats a <tt>number</tt> with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2). You can customize the format in the <tt>options</tt> hash. </p> <h4>Options</h4> <ul> <li><tt>:precision</tt> - Sets the level of precision (defaults to 3). </li> <li><tt>:separator</tt> - Sets the separator between the units (defaults to "."). </li> <li><tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ""). </li> </ul> <h4>Examples</h4> <pre> number_with_precision(111.2345) # => 111.235 number_with_precision(111.2345, :precision => 2) # => 111.23 number_with_precision(13, :precision => 5) # => 13.00000 number_with_precision(389.32314, :precision => 0) # => 389 number_with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.') # => 1.111,23 </pre> <p> You can still use <tt><a href="NumberHelper.html#M000449">number_with_precision</a></tt> with the old API that accepts the <tt>precision</tt> as its optional second parameter: </p> <pre> number_with_precision(number_with_precision(111.2345, 2) # => 111.23 </pre> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>