<commit_before># Formatting

Rails-less formatting for your unit-testable code.

*Does* currently depend on the `i18n` library for number separators.

Very much a work in progress currently.

Formats:
  * Numbers
  * Currency


## Usage

Call methods on `Formatting`:

``` ruby
Formatting.format_number(1234.567)  # => "1,234.57"
```

Or include the modules you want:

``` ruby
include Formatting::Number
format_number(1234)  # => "1,234"

include Formatting::Currency
format_currency("SEK", 1234)  # => "1,234 SEK"
```


### Number

``` ruby
Formatting.format_number(1234.567)  # => "1,234.57"
Formatting.format_number(0, blank_when_zero: true)  # => ""
Formatting.format_number(1, explicit_sign: true)  # => "+1"
```

#### Options

name                | allowed values | default | explanation
--------------------|----------------|---------|------------
thousands_separator | any string     | `I18n.t("number.format.delimiter")` if available, otherwise a non-breaking space |
decimal_separator   | any string     | `I18n.t("number.format.separator")` if available, otherwise `"."` |
round               | `false` or a non-negative integer  | `2` | Round to the given number of decimals. Don't round if given `false`.


### Currency

The currency formatter should usually be passed some object that
the currency can be determined from. The idea is that even if you
only have one currency now, you may add more later.

``` ruby
Formatting.format_currency("SEK", 1234)  # => "1,234 SEK"

item = Item.new(price: 1234, currency: "SEK")
Formatting.format_currency(item, :price)  # => "1,234 SEK"
Formatting.format_currency(company, item.price)  # => "1,234 SEK"

Formatting.defaults[:currency] = "SEK"
item = Item.new(price: 1234)  # Does not respond to "currency"
Formatting.format_currency(item, :price)  # => "1,234 SEK"

item = Item.new(price: 1234, currency: "SEK")
Formatting.format_currency(item, :price, show_currency: false)  # => "1,234"
Formatting.defaults[:show_currency] = false
Formatting.format_currency(item, :price)  # => "1,234"
```


## Installation

Add this line to your application's Gemfile:

    gem 'formatting'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install formatting


## TODO

* Use real i18n in specs so they're less fragile and ugly
* Document options
* Rename? This name is boring and also generic enough that collisions seem likely.
<commit_msg>More option docs.<commit_after># Formatting

Rails-less formatting for your unit-testable code.

*Does* currently depend on the `i18n` library for number separators.

Very much a work in progress currently.

Formats:
  * Numbers
  * Currency


## Usage

Call methods on `Formatting`:

``` ruby
Formatting.format_number(1234.567)  # => "1,234.57"
```

Or include the modules you want:

``` ruby
include Formatting::Number
format_number(1234)  # => "1,234"

include Formatting::Currency
format_currency("SEK", 1234)  # => "1,234 SEK"
```


### Number

``` ruby
Formatting.format_number(1234.567)  # => "1,234.57"
Formatting.format_number(0, blank_when_zero: true)  # => ""
Formatting.format_number(1, explicit_sign: true)  # => "+1"
```

#### Options

name                | default | explanation
--------------------|---------|------------
thousands_separator | `I18n.t("number.format.delimiter")` if available, otherwise a non-breaking space |
decimal_separator   | `I18n.t("number.format.separator")` if available, otherwise `"."` |
round               | `false` or a non-negative integer  | `2` | Round to the given number of decimals. Don't round if given `false`.


### Currency

The currency formatter should usually be passed some object that
the currency can be determined from. The idea is that even if you
only have one currency now, you may add more later.

``` ruby
Formatting.format_currency("SEK", 1234)  # => "1,234 SEK"

item = Item.new(price: 1234, currency: "SEK")
Formatting.format_currency(item, :price)  # => "1,234 SEK"
Formatting.format_currency(company, item.price)  # => "1,234 SEK"

Formatting.defaults[:currency] = "SEK"
item = Item.new(price: 1234)  # Does not respond to "currency"
Formatting.format_currency(item, :price)  # => "1,234 SEK"

item = Item.new(price: 1234, currency: "SEK")
Formatting.format_currency(item, :price, show_currency: false)  # => "1,234"
Formatting.defaults[:show_currency] = false
Formatting.format_currency(item, :price)  # => "1,234"
```


## Installation

Add this line to your application's Gemfile:

    gem 'formatting'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install formatting


## TODO

* Use real i18n in specs so they're less fragile and ugly
* Document options
* Rename? This name is boring and also generic enough that collisions seem likely.

{'fraction_non_alphanumeric': 0.14673684210526317, 'fraction_numerical': 0.039578947368421054, 'mean_word_length': 3.8828365878725593, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4056482', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1486, 'n_words': 468}