Commit Graph

35 Commits

Author SHA1 Message Date
Pan GaoYong
4e977da541 number_to_phone formats number with regexp
By default, this method formats US number. This commit extends its
functionality to format number for other countries with a custom regular
expression.

    number_to_phone(18812345678, pattern: /(\d{3})(\d{4})(\d{4})/)
    # => 188-1234-5678

The output phone number is divided into three groups, so the regexp
should also match three groups of numbers.
2016-04-02 20:13:22 +08:00
Gert Goet
0a96195119 Show correct return types for examples [ci skip] 2016-02-20 00:44:34 +01:00
Eileen M. Uchitelle
85048e90ab Merge pull request #22759 from akshay-vishnoi/human-size-helper
Add support for Petabyte and Exabyte in number to human size
2015-12-27 09:56:43 -05:00
Derek Prior
d86c110b93
Add caveat to number_to_currency docs [ci skip]
I've worked on a few applications that have gone through the
internationalization process and had issues because they were using
`number_to_currency`. The minute a user is allowed to change their
locale, they can change the price displayed on a page from 10 US dollars
to 10 Mexican Pesos, which is far from the same amount of money.

Unlike other helpers that rely on i18n, `number_to_currency` does not
produce equivalent results when the locale is changed.

As I've explained this to a few groups of developers now, I thought it
might make for a good caveat in the docs.
2015-12-23 14:04:52 -05:00
Akshay Vishnoi
3d1f95d0e8 Add support for Petabyte and Exabyte in number to human size 2015-12-22 23:07:06 +05:30
amitkumarsuroliya
05c610df9a Fixed ActiveSupport::NumberHelper Outputs [ci skip] 2015-10-11 07:41:15 +05:30
amitkumarsuroliya
f077cc6f41 Correcting output of number_to_percentage example in number_helper [ci skip]
Here is correct output of `number_to_percentage(100, format: "%n  %”)`
2015-09-22 07:38:02 +05:30
Robin Dupret
7a27de2bb0 Tiny documentation improvements [ci skip] 2015-08-28 15:33:48 +02:00
Vipul A M
7f23c5d524 - Extracted DELIMITED_REGEX to delimited_regex method and made use of user passed options[:delimited_regex] if available. Changed DELIMITED_REGEX to DEFAULT)DELIMITED_REGEX to signify what it means.
- Added tests for number to delimited and number to currency in both actionview and activesupport.

Changes

Changes
2015-08-28 11:34:17 +05:30
Jean Boussier
7081106457 Deprecate the :prefix option of number_to_human_size 2015-08-10 23:15:09 -04:00
Yu Haidong
d4f2c3adb3 Fix spelling [ci skip] 2015-03-04 21:54:54 +08:00
Rafael Mendonça França
2d2bcde3cb Merge pull request #12067 from jackxxu/keep_precision
Enable number_to_percentage to keep the number's precision by allowing :precision option value to be nil

Conflicts:
	activesupport/CHANGELOG.md
	activesupport/lib/active_support/number_helper.rb
	activesupport/test/number_helper_test.rb
2015-02-06 16:21:28 -02:00
Ryunosuke SATO
44260581be Fix doc markup for NumberHelper [ci skip]
The character "*" is unnecessary in option candidates.
This incorrect markup was injected in e8c9aeca .
2014-10-29 00:32:43 +09:00
Akshay Vishnoi
294ddff512 [ci skip] Add Docs for strip_insignificant_zeros option in number helpers 2014-06-10 23:07:49 +05:30
Akshay Vishnoi
9bb0f1e08a [ci skip] Correct output and use valid options of number helpers 2014-06-10 22:16:13 +05:30
Carlos Antonio da Silva
443c31f8fc Nodoc missing number helper classes in AS [ci skip]
These classes are not meant to be public.
2013-12-11 20:01:54 -02:00
Rafael Mendonça França
5c04ca87d8 Make execute priave API 2013-12-02 22:32:21 -02:00
Rafael Mendonça França
d3b93e403b Make load of NumberHelper thread safe 2013-12-02 22:12:36 -02:00
Rafael Mendonça França
fc73ebf332 Merge pull request #10996 from mattdbridges/number-helper-refactor
Refactor and clean up number helpers

Conflicts:
	activesupport/lib/active_support/number_helper.rb
2013-12-02 20:50:03 -02:00
Vipul A M
67c2525d59 Minor Refactoring to NumberHelper#number_to_human
* Use destructive `map` and `sort_by` to save extra object creation.
* Create `INVERTED_DECIMAL_UNITS.invert` constant instead of repeatedly doing `DECIMAL_UNITS.invert`
2013-10-14 23:52:06 +05:30
Mac Martine
ee145c025c Fix typo in number_to_human docs: you -> your 2013-09-24 15:13:23 -07:00
Jack Xu
24a7e60943 Enable number_to_percentage to keep the number's precision by allowing :precision to be nil
number_helper.number_to_percentage(1000, precision: nil) # => "1000%"
2013-08-28 23:46:53 -05:00
Rafael Mendonça França
816b35e781 Fix examples of number_to_percentage
[ci skip]
2013-07-18 10:33:33 -03:00
Matt Bridges
2da9d67c27 Extract ActiveSupport::NumberHelper methods to classes
Due to the overall complexity of each method individually as well as the
global shared private module methods, this pulls each helper into it's
own converter class inheriting from a generic `NumberBuilder` class.

* The `NumberBuilder` class contains the private methods needed for each helper
method an eliminates the need for special definition of specialized private
module methods.
* The `ActiveSupport::NumberHelper::DEFAULTS` constant has been moved
into the `NumberBuilder` class because the `NumberBuilder` is the only
class which needs access to it.
* For each of the builders, the `#convert` method is broken down to
smaller parts and extracted into private methods for clarity of purpose.
* Most of the mutation that once was necessary has now been eliminated.
* Several of the mathematical operations for percentage, delimited, and
rounded have been moved into private methods to ease readability and
clarity.
* Internationalization is still a bit crufty, and definitely could be
improved, but it is functional and a bit easier to follow.

The following helpers were extracted into their respective classes.

* `#number_to_percentage` -> `NumberToPercentageConverter`
* `#number_to_delimited` -> `NumberToDelimitedConverter`
* `#number_to_phone` -> `NumberToPhoneConverter`
* `#number_to_currency` -> `NumberToCurrencyConverter`
* `#number_to_rounded` -> `NumberToRoundedConverter`
* `#number_to_human_size` -> `NumberToHumanSizeConverter`
* `#number_to_human` -> `NumberToHumanConverter`
2013-07-01 12:31:36 -05:00
Vipul A M
e772daad20 refactor number helper 2013-04-25 00:59:16 +05:30
hoffm
4e3ceedeba Address edge case for number_to_human with units option.
ActiveSupport::NumberHelper#number_to_human now returns the number unaltered when
the units hash does not contain the needed key, e.g. when the number provided is less
than the largest key provided.
2013-02-22 08:25:51 -05:00
Francesco Rodriguez
f4e180578c update some AS code examples to 1.9 hash syntax [ci skip] 2012-09-12 13:47:49 -05:00
Carlos Antonio da Silva
47b4d13c8d Ensure I18n format values always have precedence over defaults
Always merge I18n format values, namespaced or not, over the default
ones, to ensure I18n format defaults will have precedence over our
namespaced values.

Precedence should happen like this:

    default :format
    default :namespace :format
    i18n    :format
    i18n    :namespace :format

Because we cannot allow our namespaced default to override a I18n
:format config - ie precision in I18n :format should always have higher
precedence than our default precision for a particular :namespace.

Also simplify default format options logic.
2012-08-11 00:20:19 -03:00
Carlos Antonio da Silva
a9dccda936 Fallback to :en locale instead of handling a constant with defaults
Action Pack already comes with a default locale fine for :en, that is
always loaded. We can just fallback to this locale for defaults, if
values for the current locale cannot be found.

Closes #4420, #2802, #2890.
2012-08-11 00:20:19 -03:00
Carlos Antonio da Silva
36d863ce51 Move constants to the top, remove freeze 2012-06-24 19:30:05 -03:00
Carlos Antonio da Silva
06d50b9d73 Remove some not used variables and improve code a bit 2012-06-24 19:30:05 -03:00
Francesco Rodriguez
1790b234e4 add :nodoc: to AS::NumberHelper private methods [ci skip] 2012-06-05 15:53:20 -05:00
Carlos Antonio da Silva
400c5fefcf Review requires from number helper
Some of these requires are now only necessary in
ActiveSupport::NumberHelper. Add hash/keys require due to symbolize_keys
usage in number helpers. Also remove some whitespaces.

Closes #6414
2012-05-28 23:55:20 -03:00
Jared Beck
371508c240 Fix handling of negative zero in number_to_currency 2012-05-28 04:42:53 -04:00
Andrew Mutz
155cd5e6b5 Moving NumberHelpers from ActionView to ActiveSupport 2012-05-27 18:14:21 -07:00