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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YWI5ZTFkMTdmMTc0NGJhYTdlYzJjMTU2NDFhZTAwNTliNjUyYTRkZQ==
4
+ YzE3NDZkOGE2ODY0ZDgxNWViNGEzNGY0NTMyNjBmMjg5MTBhNTI1MA==
5
5
  data.tar.gz: !binary |-
6
- OGU1OWE2YWY5Y2ZmMThjYTZhMDQzMzgyNDQ1ZDNiOTNkMTFmYmY5Yw==
6
+ NmM1M2JjNjQ1NDA0N2FjYzI0NmU3ZTc1YWQ4M2FlYTgxZGJlNWEyNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NzQ5MGY5MDQ0ZDIxMzc3ZGE3YzcyNDQxZDU5MmJmM2IyNTBlYWIxYTgwYWNi
10
- YTdlMWMwOWZlNzI1Y2MzY2Q2MzUyMzNiMjIwOGYxMDg1MTdmYTEyMjgzZDgw
11
- MzQ4NmRmMTY5MjU2OTY1ODJkNmFhOWEyOGJiOTNlZmE3NGI0YmQ=
9
+ NTBiNDc1MTMzNzg5NmU2YjliNjhjZDYyOThjODhhMDVmM2ExNTAzZmQ3NDg4
10
+ MWU4YmNiNDcyODNhZjgwZDNmYzhiNDE3ZmI0OGQ5ZTNiMzU5OWYzNmNlNDBh
11
+ Y2YzYTQ3YTlhZjdkNDMzY2Y0MWM1ODllN2Y1NzllYzAxY2JmMzU=
12
12
  data.tar.gz: !binary |-
13
- Yjc3MDk0NTY5YTM4ZmM5YWI1ZGM3ZTkyMTAzNTdiYmY3Y2UzNjk4YzhmNDUz
14
- MjNlZDA3ODc3MzkzZmFkMTVmZTQ4MzQ3MDMwNzEwNTEwNzQ1MDQ2NWY5ZGVj
15
- MTEzOTIxODkzYTJlMDc4ODFlYzAzNmI0Zjc5ZDJmNGU2NjIzOTk=
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 | 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`.
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
@@ -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 = 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
- @explicit_sign = opts.fetch(:explicit_sign, false)
23
- @blank_when_zero = opts.fetch(:blank_when_zero, false)
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
@@ -1,3 +1,3 @@
1
1
  module Formatting
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.14"
3
3
  end
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"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formatting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh