Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented Number#as_human #61

Merged
merged 3 commits into from
Aug 16, 2015
Merged

implemented Number#as_human #61

merged 3 commits into from
Aug 16, 2015

Conversation

druzn3k
Copy link
Collaborator

@druzn3k druzn3k commented Aug 15, 2015

This commit closes issue #25. Ugly but implements the functionalities.

This commit closes issue #25.
@duijf
Copy link
Collaborator

duijf commented Aug 15, 2015

Currently, because of the way the @doc statements are structured, all documentation is bound to the as_human! function. The non-throwing version doesn't have any docs associated with it.

Redid them for you (with the format I was experimenting with yesterday — this seems to look best):

  @doc ~S"""
  Formats and approximates `number` for human readability.

  `1200000000` becomes `"1.2 Billion"`. This is useful as larger numbers become
  harder to read.

  See `as_human_size` if you want to print a file size.

  You can also define you own unit-quantifier names if you want to use other
  decimal units (eg.: 1500 becomes "1.5 kilometers", 0.150 becomes
  “150 milliliters”, etc). You may define a wide range of unit quantifiers, even
  fractional ones (centi, deci, mili, etc).

  # Options

  - `:locale` (atom) --- Locale to be used for formatting. *Default:*
  current locale.
  - `:precision` (integer) --- Precision of the number. *Default:* `3`
  - `:significant` - If true, precision will be the # of significant_digits. If
  false, the # of fractional digits (defaults to true)
  - `:separator` (string) --- Separator between the fractional and integer
  digits. *Default:* `"."`
  - `:delimiter` (string) --- Thousands delimiter. *Default:* `""`
  -`:units` (keyword list/string) --- Keyword list of unit quantifier names,
  *or* a string containing an i18n scope pointing to it.
  - `:format` (string) --- Format of the output string. `%u` is the quantifier,
  `%n` is the number. *Default:* `"%n %u"`

  # i18n

  This function takes a keyword list of quantifier names to use for formatting.
  It supports the following keys:

  - `:unit`
  - `:ten`
  - `:hundred`
  - `:thousand`
  - `:million`
  - `:billion`
  - `:trillion`
  - `:quadrillion`
  - `:deci`
  - `:centi`
  - `:milli`
  - `:micro`
  - `:nano`
  - `:pico`
  - `:femto`

  # Examples

      iex> Number.as_human(123)
      "123"

      iex> Number.as_human(1234)
      "1.23 Thousand"

      iex> Number.as_human(12345)
      "12.3 Thousand"

      iex> Number.as_human(1234567)
      "1.23 Million"

      iex> Number.as_human(1234567890)
      "1.23 Billion"

      iex> Number.as_human(1234567890123)
      "1.23 Trillion"

      iex> Number.as_human(1234567890123456)
      "1.23 Quadrillion"

      iex> Number.as_human(1234567890123456789)
      "1230 Quadrillion"

      iex> Number.as_human(489939, precision: 2)
      "490 Thousand"

      iex> Number.as_human(489939, precision: 4)
      "489.9 Thousand"

      iex> Number.as_human(1234567, precision: 4, significant: false)
      "1.2346 Million"

      iex> Number.as_human(1234567, precision: 1, separator: ",", significant: false)
      "1,2 Million"

      iex> Number.as_human(500000000, precision: 5)
      "500 Million"

      iex> Number.as_human(12345012345, significant: false)
      "12.345 Billion"

      iex> Number.as_human!("abc")
      ** (ArithmeticError) bad argument in arithmetic expression
  """

  def as_human(...) do

  [...]

  @doc ~S"""
  Throwing version of `as_human`, raises if the input is not a valid number.
  """
  def as_human!(...) do

  [...]

@druzn3k
Copy link
Collaborator Author

druzn3k commented Aug 16, 2015

corrected errors

druzn3k added a commit that referenced this pull request Aug 16, 2015
implemented Number#as_human
@druzn3k druzn3k merged commit 031e178 into mykewould:master Aug 16, 2015
@duijf
Copy link
Collaborator

duijf commented Aug 16, 2015

👍

opts = Option.combine!(opts, @as_human)
{exp, unit, sign} = closest_size_and_sign(number)

precision = opts[:precision]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

precision = Math.max(opts[:precision], 0)

:shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants