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

add precision option #134

Merged
merged 2 commits into from
Dec 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
rvm:
- 2.2
- 2.3
- jruby-9.0.5.0
- rbx
- jruby-9.1.6.0
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 0.11.6 (Next)
## 0.10.6 (Next)

### Features
Copy link
Collaborator

Choose a reason for hiding this comment

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

Put this line back ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

* Your contribution here.
* Add option `precision` to `to_words`. \[[#134](https://github.com/kslazarev/numbers_and_words/issues/134)\] \([@neodelf](https://github.com/neodelf)\) \(assignee: [@neodelf](https://github.com/neodelf)\)

### Bugs
* Fix README typo for 231 in French. \(assignee: [@poilon](https://github.com/poilon)\)
Expand Down
9 changes: 9 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ I18n kütüphanesi ile sayıları yazıya çevirir.
I18n.with_locale(:hu) { 21.to_words ordinal: true }
=> "huszonegyedik"

== Other options / Другие опции

* precision

You may pass argument :precision that added zeros to the end of float number:

I18n.with_locale(:ru) { 0.1.to_words precision: 3 }
=> ноль целых и десять сотых

== Requirements / Требования / Configuration Requise

* 1.9.3 <= Ruby (compatible with/совместимость с/compatible avec Ruby 1.9, JRuby and/и/et Rubinius);
Expand Down
16 changes: 14 additions & 2 deletions lib/numbers_and_words/wrappers/float.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
module NumbersAndWords
module Wrappers
class Float
ZERO_SYMBOL = '0'

attr_accessor :number

def initialize number
@number = number
end

def to_words options = {}
@options = options
words = []
words << integral_part_with(options)
words << fractional_part_with(options) unless fractional_part_is_nil?
Expand All @@ -16,6 +19,8 @@ def to_words options = {}

private

attr_accessor :options

def parts
number.to_s.split '.'
end
Expand All @@ -25,7 +30,9 @@ def integral_part
end

def fractional_part
parts.last
part = parts.last
part += ZERO_SYMBOL*(precision - part.length) if precision
part
end

def integral_part_with options
Expand All @@ -41,12 +48,17 @@ def integral_options
end

def fractional_options
{:fractional => {:length => fractional_part.length}}
length = precision || fractional_part.length
{:fractional => {:length => length}}
end

def fractional_part_is_nil?
0 == fractional_part.to_i
end

def precision
options.fetch(:precision, nil)
end
end
end
end
13 changes: 13 additions & 0 deletions spec/numbers_and_words/float/fixture_examples/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ to_words:
21.77: двадцать одна целая и семьдесят семь сотых
111.999: сто одиннадцать целых и девятьсот девяносто девять тысячных
4242.7463: четыре тысячи двести сорок двe целых и семь тысяч четыреста шестьдесят три десяти тысячных
fractions_with_precision:
options:
:precision: 2
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this be precision: 2? (as well as the others)

Copy link
Contributor Author

@Neodelf Neodelf Dec 12, 2016

Choose a reason for hiding this comment

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

Unfortunately, no

0.1: ноль целых и десять сотых
options:
:precision: 3
0.1: ноль целых и одна тысяча десять десяти тысячных
0.11: ноль целых и одна тысяча сто десяти тысячных
options:
:precision: 4
0.1: ноль целых и одна тысяча десяти тысячных
0.11: ноль целых и одна тысяча сто десяти тысячных
0.101: ноль целых и одна тысяча десять десяти тысячных