Skip to content

Commit

Permalink
Add precision option to to_words (kslazarev#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neodelf authored and dblock committed Dec 13, 2016
1 parent 0c3a3af commit c24a2ed
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
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
* 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
0.1: ноль целых и десять сотых
options:
:precision: 3
0.1: ноль целых и одна тысяча десять десяти тысячных
0.11: ноль целых и одна тысяча сто десяти тысячных
options:
:precision: 4
0.1: ноль целых и одна тысяча десяти тысячных
0.11: ноль целых и одна тысяча сто десяти тысячных
0.101: ноль целых и одна тысяча десять десяти тысячных

0 comments on commit c24a2ed

Please sign in to comment.