pretty_floats 1.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 +8 -6
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac694981f780e140d427ed58db1c226cd1bc861b1854301635d878fcf1b1072e
4
- data.tar.gz: 0ac9e1aef495355ee7b2ad206c9bc038b01bbbd5fb33f35b585a7f4a98ee4e5e
3
+ metadata.gz: c55d7ddbb44134dd3c5b5d1798f27cf6f2f7244aad99cde2713a09d60298661b
4
+ data.tar.gz: 2961c06fe81b65aa3ac9ec132eceb0de2173e2ed226697601bf7c0d9c0ea01f0
5
5
  SHA512:
6
- metadata.gz: 5d403cc2ed314750ad9cc952d2de7fd9ac0b5da75662cb74963b28cbd46ac40c045b27f7480f1c75e4c554504b65e0905a9046770cd4054aba26b257a01930bd
7
- data.tar.gz: ac96e21461da3027a84bea162bb2e8395a071ae301d36a6f78e5c524406141bd64cc5764b2015d763c21fb9494ca73d6c815baea2e400df35102738943fe6f33
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
- def prettify(place_sep=',', radix_sep='.')
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 = self.to_s.split('.').last.length
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_floats
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-18 00:00:00.000000000 Z
11
+ date: 2023-09-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: a small utility to format floating point numbers for easier reading
14
14
  email: jefflunt@gmail.com