pretty_bigdecimals 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/pretty_bigdecimals.rb +31 -0
- metadata +40 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f5938cb048c2c69f4098618a2c591c649efee03d8d21588ebc6eb896b92f7ebb
|
4
|
+
data.tar.gz: cca9b899e3354f9e2eeffaa189e789dd069dd5f8924ba4165b7555b7678fa8b2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a276710aba790a1a66bd440088ed25c394003c70f07495dbaae1755c673c0d265967d7bf6ae2007d0c03bbd7315651833020028a94d4e251a365315816a68ff
|
7
|
+
data.tar.gz: 32e43f80091a3dae9c8671405a88ac3d315bab9a43e115cebb933493120aa2e789c8a8fb9860f0fa373e4981f0f9a4713f0f5b208dfdc42d401a29eaff783520
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
class BigDecimal
|
4
|
+
# changes numbers like 1234567 to "1,234,567"
|
5
|
+
# or:
|
6
|
+
#
|
7
|
+
# > -13321.23.prettify(num_places: 2)
|
8
|
+
# => "-13,321.77"
|
9
|
+
def prettify(place_sep: ',', radix_sep: '.', num_places: -1)
|
10
|
+
sign = self < 0 ? '-' : ''
|
11
|
+
|
12
|
+
int = self
|
13
|
+
.abs
|
14
|
+
.to_i
|
15
|
+
.to_s
|
16
|
+
.chars
|
17
|
+
.reverse
|
18
|
+
.each_slice(3)
|
19
|
+
.map{|slice| slice.join }
|
20
|
+
.join(place_sep)
|
21
|
+
.reverse
|
22
|
+
|
23
|
+
max_precision = num_places == -1 ? self.to_s.split('.').last.length : num_places
|
24
|
+
bd = self % 1
|
25
|
+
bd = ("%.#{max_precision}f" % bd)
|
26
|
+
.split('.')
|
27
|
+
.last
|
28
|
+
|
29
|
+
sign + int + radix_sep + bd
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pretty_bigdecimals
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Lunt
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-02-18 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: a small utility to format BigDecimal numbers for easier reading
|
13
|
+
email: jefflunt@gmail.com
|
14
|
+
executables: []
|
15
|
+
extensions: []
|
16
|
+
extra_rdoc_files: []
|
17
|
+
files:
|
18
|
+
- lib/pretty_bigdecimals.rb
|
19
|
+
homepage: https://github.com/jefflunt/pretty_bigdecimals
|
20
|
+
licenses:
|
21
|
+
- MIT
|
22
|
+
metadata: {}
|
23
|
+
rdoc_options: []
|
24
|
+
require_paths:
|
25
|
+
- lib
|
26
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
requirements: []
|
37
|
+
rubygems_version: 3.6.3
|
38
|
+
specification_version: 4
|
39
|
+
summary: extends the BigDecimal class to add a few methods for inserting places separators
|
40
|
+
test_files: []
|