formatting 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +9 -8
- data/lib/formatting/number.rb +9 -8
- data/lib/formatting/version.rb +1 -1
- data/spec/number_spec.rb +10 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YzE3NDZkOGE2ODY0ZDgxNWViNGEzNGY0NTMyNjBmMjg5MTBhNTI1MA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NmM1M2JjNjQ1NDA0N2FjYzI0NmU3ZTc1YWQ4M2FlYTgxZGJlNWEyNQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NTBiNDc1MTMzNzg5NmU2YjliNjhjZDYyOThjODhhMDVmM2ExNTAzZmQ3NDg4
|
10
|
+
MWU4YmNiNDcyODNhZjgwZDNmYzhiNDE3ZmI0OGQ5ZTNiMzU5OWYzNmNlNDBh
|
11
|
+
Y2YzYTQ3YTlhZjdkNDMzY2Y0MWM1ODllN2Y1NzllYzAxY2JmMzU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YjI2YWRiYTBlZTY4ZjBlOWZkZDdiYTgwYTlhNDQ1NTA0YjE3MmI4MTM4Mjhk
|
14
|
+
NzhhMTk1OGUzYzNhM2NmNjc3MGJhNzEyMmFhYTE3OWZhYWNkYzZjYjZkNzQw
|
15
|
+
YWM1MzYzMDA5N2IyOWQxNDhkOGQwNzFkOTM3NzQxZTA4YmY4NDI=
|
data/README.md
CHANGED
@@ -40,14 +40,15 @@ Formatting.format_number(1, explicit_sign: true) # => "+1.00"
|
|
40
40
|
|
41
41
|
#### Options
|
42
42
|
|
43
|
-
name
|
44
|
-
|
45
|
-
thousands_separator
|
46
|
-
decimal_separator
|
47
|
-
round
|
48
|
-
blank_when_zero
|
49
|
-
min_decimals
|
50
|
-
explicit_sign
|
43
|
+
name | default | explanation
|
44
|
+
------------------------|----------------------------------------------------------------------------------|------------
|
45
|
+
thousands_separator | `I18n.t("number.format.delimiter")` if available, otherwise a non-breaking space |
|
46
|
+
decimal_separator | `I18n.t("number.format.separator")` if available, otherwise `"."` |
|
47
|
+
round | `2` | Round to the given number of decimals. Don't round if given `false`.
|
48
|
+
blank_when_zero | `false` | If `true`, returns `""` for a zero value.
|
49
|
+
min_decimals | `2` | Show at least that number of decimals. Don't enforce if given `false`.
|
50
|
+
explicit_sign | `false` | If `true`, prefixes positive values with a `"+"`. Doesn't prefix `0`.
|
51
|
+
decimals_on_integers | `true` | If `false`, integers won't include decimals.
|
51
52
|
|
52
53
|
|
53
54
|
### Currency
|
data/lib/formatting/number.rb
CHANGED
@@ -10,17 +10,18 @@ module Formatting
|
|
10
10
|
class FormatNumber
|
11
11
|
attr_private :input_number,
|
12
12
|
:thousands_separator, :decimal_separator,
|
13
|
-
:round, :min_decimals, :explicit_sign, :blank_when_zero
|
13
|
+
:round, :min_decimals, :decimals_on_integers, :explicit_sign, :blank_when_zero
|
14
14
|
|
15
15
|
def initialize(input_number, opts)
|
16
16
|
@input_number = input_number
|
17
17
|
|
18
|
-
@thousands_separator
|
19
|
-
@decimal_separator
|
20
|
-
@round
|
21
|
-
@min_decimals
|
22
|
-
@
|
23
|
-
@
|
18
|
+
@thousands_separator = opts.fetch(:thousands_separator) { default_thousands_separator }
|
19
|
+
@decimal_separator = opts.fetch(:decimal_separator) { default_decimal_separator }
|
20
|
+
@round = opts.fetch(:round, 2)
|
21
|
+
@min_decimals = opts.fetch(:min_decimals, 2)
|
22
|
+
@decimals_on_integers = opts.fetch(:decimals_on_integers, true)
|
23
|
+
@explicit_sign = opts.fetch(:explicit_sign, false)
|
24
|
+
@blank_when_zero = opts.fetch(:blank_when_zero, false)
|
24
25
|
end
|
25
26
|
|
26
27
|
def format
|
@@ -48,7 +49,7 @@ module Formatting
|
|
48
49
|
integer = "+#{integer}" if number > 0
|
49
50
|
end
|
50
51
|
|
51
|
-
if min_decimals
|
52
|
+
if min_decimals && (has_decimals || decimals_on_integers)
|
52
53
|
decimals ||= "0"
|
53
54
|
decimals = decimals.ljust(min_decimals, "0")
|
54
55
|
end
|
data/lib/formatting/version.rb
CHANGED
data/spec/number_spec.rb
CHANGED
@@ -110,6 +110,16 @@ describe Formatting do
|
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
113
|
+
context "decimals on integers" do
|
114
|
+
it "defaults to adding decimals" do
|
115
|
+
expect_formatted(12).to eq "12.00"
|
116
|
+
end
|
117
|
+
|
118
|
+
it "can be turned off" do
|
119
|
+
expect_formatted(12, decimals_on_integers: false).to eq "12"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
113
123
|
context "explicit sign" do
|
114
124
|
it "is not included by default" do
|
115
125
|
expect_formatted(1).to eq "1.00"
|