pretty_floats 2.0.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pretty_floats.rb +7 -5
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f32d365b5a3e263692ea64cfc4671722c6e4ca03a944e8a7bd0e7f9ee223c4b
4
- data.tar.gz: ea4dd60168199900edefddd805870793b6ddbf613d9a950531ef45d37348404e
3
+ metadata.gz: c55d7ddbb44134dd3c5b5d1798f27cf6f2f7244aad99cde2713a09d60298661b
4
+ data.tar.gz: 2961c06fe81b65aa3ac9ec132eceb0de2173e2ed226697601bf7c0d9c0ea01f0
5
5
  SHA512:
6
- metadata.gz: 626448b6757eb7a533fa8d4ea86848b8ed28fd1e607d43a8f7dc9097e5bec70537fef232c3bae5afc8f0f1b631d055c8cf908915092f1fa45df3c1b9bc657fb9
7
- data.tar.gz: 9d5ae8fac0c7199c07dff986fa8fb11132eef9b80e5cfea53090a89b2f939198e9bb28e7d8def31f7b12a6a20c3f29bf6431155e875ff191376a92ddd05a2176
6
+ metadata.gz: 4f9add340af35497dd115f8f0d6db78a1b44d93b2ed49d133c76ddda4c404771528a4d5203427a30b26658e6e39244a3c38e3685c360cf81c57fb1865a857961
7
+ data.tar.gz: 648e44584e753f965bc6a87108c4acd524e0fe97cc3b4c7605a10f5dce7fbd69176765a7674f9abf53fa628e660b566a29fe4be760ffc1412a1200ab8fae8bed
data/lib/pretty_floats.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  class Float
2
2
  # changes numbers like 1234567 to "1,234,567"
3
3
  def prettify(place_sep: ',', radix_sep: '.', num_places: -1)
4
+ sign = self < 0 ? '-' : ''
5
+
4
6
  int = self
7
+ .abs
5
8
  .to_i
6
9
  .to_s
7
10
  .chars
@@ -11,13 +14,12 @@ class Float
11
14
  .join(place_sep)
12
15
  .reverse
13
16
 
14
- max_digits = num_places == -1 ? self.to_s.split('.').last.length : num_places
15
- float = (self % 1)
16
- .round(max_digits)
17
- .to_s
17
+ max_precision = num_places == -1 ? self.to_s.split('.').last.length : num_places
18
+ float = self % 1
19
+ float = ("%.#{max_precision}f" % float)
18
20
  .split('.')
19
21
  .last
20
22
 
21
- int + radix_sep + float
23
+ sign + int + radix_sep + float
22
24
  end
23
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_floats
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt