Ruby on Rails
HowToUseNumberHelper (Version #19)

Helpwhat?

As we all well know, Rails does a lot of work for us (yippie! more time for ps2!) in the form of Helpers.
ex. for helpers is the FormHelper for form fields creation

aaannndd?

Each one of the pre-made helpers, desigend to preforms an action which in other web-development frameworks and languages would take you hours/days/years (if you are married). the only objective of these pre-methods is to DRY your code in a way that it is also readable, and also correct (not all of us are super-programmers).

you said something about number help?

yes, the NumberHelper provids basic methods for converting numbers to a string form such as: phone number, percentage or money (there are more, i’m just lazy)

show me the money!

NumberHelper methods are as follows:

human_size(size)

Returns a readable file-size string

human_size(777)        => 777 bytes
human_size(7777)       => 7.7 KB

number_to_currency(number,options = {})

Formats the number into a currency readable string, output can be formatted using the options parameter

number_to_currency(1234.50) => $1,234.50
number_to currency(1234.50,{:unit => "&pound", :delimiter => "-"}) => &pound1-234.50

number_to_percentage(number, options= {})

Formats the number into a precentage string. output can be formatted using the options parameter.

number_to_percentage(100)  => 100.000%
number_to_percentage(100, {:precision => 0}) => 100%

number_to_phone(number, options = {})

Formats a number into a US phone number string. output can be formatted using the options parameter.
The area code can be surrounded by parentheses by setting rea_code to true; default is false The delimiter can be set using elimiter; default is ”-”

number_to_phone(1235551234)   => 123-555-1234
number_to_phone(1235551234, {:area_code => true})   => (123) 555-1234

number_with_delimiter(number, delimiter=”,”)

Formats a number to a delimeterd string

number_with_delimiter(1234) => 1,234

number_with_precision(number, precision=3)

Formats a number to a precised string

number_with_precision(1200.1234) => 1200.123

—Elad Meidar elad@creopolis.com

Questions

How do I format a 2 digit number to a 5 digit number? ex. from 12 to 00012

Like this:

"%05.0f" % 12   =>   "00012"

Comments

Great piece of work, very helpful

Helpwhat?

As we all well know, Rails does a lot of work for us (yippie! more time for ps2!) in the form of Helpers.
ex. for helpers is the FormHelper for form fields creation

aaannndd?

Each one of the pre-made helpers, desigend to preforms an action which in other web-development frameworks and languages would take you hours/days/years (if you are married). the only objective of these pre-methods is to DRY your code in a way that it is also readable, and also correct (not all of us are super-programmers).

you said something about number help?

yes, the NumberHelper provids basic methods for converting numbers to a string form such as: phone number, percentage or money (there are more, i’m just lazy)

show me the money!

NumberHelper methods are as follows:

human_size(size)

Returns a readable file-size string

human_size(777)        => 777 bytes
human_size(7777)       => 7.7 KB

number_to_currency(number,options = {})

Formats the number into a currency readable string, output can be formatted using the options parameter

number_to_currency(1234.50) => $1,234.50
number_to currency(1234.50,{:unit => "&pound", :delimiter => "-"}) => &pound1-234.50

number_to_percentage(number, options= {})

Formats the number into a precentage string. output can be formatted using the options parameter.

number_to_percentage(100)  => 100.000%
number_to_percentage(100, {:precision => 0}) => 100%

number_to_phone(number, options = {})

Formats a number into a US phone number string. output can be formatted using the options parameter.
The area code can be surrounded by parentheses by setting rea_code to true; default is false The delimiter can be set using elimiter; default is ”-”

number_to_phone(1235551234)   => 123-555-1234
number_to_phone(1235551234, {:area_code => true})   => (123) 555-1234

number_with_delimiter(number, delimiter=”,”)

Formats a number to a delimeterd string

number_with_delimiter(1234) => 1,234

number_with_precision(number, precision=3)

Formats a number to a precised string

number_with_precision(1200.1234) => 1200.123

—Elad Meidar elad@creopolis.com

Questions

How do I format a 2 digit number to a 5 digit number? ex. from 12 to 00012

Like this:

"%05.0f" % 12   =>   "00012"

Comments

Great piece of work, very helpful