hex_values 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -25,21 +25,38 @@ Or install it yourself as:
25
25
 
26
26
  require the gem:
27
27
 
28
- require 'hex_values'
29
-
28
+ ```ruby
29
+ require 'hex_values'
30
+ ```
30
31
  And then transform any float to a hexadecimal:
31
32
 
32
- irb> 234.25.to_hex
33
- => "EA.4"
33
+ ```ruby
34
+ 234.25.to_hex
35
+ # => "EA.4"
36
+ ```
34
37
 
35
38
  Or transform any hexadecimal to the corresponding float:
36
39
 
37
- irb> "EA.4".to_float
38
- => 234.25
40
+ ```ruby
41
+ "EA.4".to_float
42
+ # => 234.25
43
+ ```
44
+
45
+ ### From float to Hex
46
+
47
+ The "to_hex" method allows a precision parameter to set the maximum number of decimals to calculate (default 14).
39
48
 
49
+ Bear in mind that the larger the number, the larger the amount of time used to get the number:
50
+
51
+ ```ruby
52
+ 1875.37.to_hex(50)
53
+ # => 753.5EB851EB851EB851EB851EB851EB851EB851EB851EB851EB85
54
+ ```
40
55
 
41
56
  ## Contributing
42
57
 
58
+ There are many, many ways to contribute to this project...but I will only explain two :smile:
59
+
43
60
  ### Forking the project
44
61
 
45
62
  1. Fork it
@@ -1,3 +1,3 @@
1
1
  module HexValues
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/hex_values.rb CHANGED
@@ -3,12 +3,14 @@ require "hex_values/version"
3
3
  module HexValuesFromFloat
4
4
  MAX_DECIMALS = 14
5
5
 
6
- def to_hex
6
+ def to_hex(precision = MAX_DECIMALS)
7
+ precision = MAX_DECIMALS if precision < 0
8
+
7
9
  num,dec = get_hex_value(self)
8
10
  first = num
9
11
  second = ""
10
12
  iterations = 0
11
- while dec > 0 && iterations < MAX_DECIMALS
13
+ while dec > 0 && iterations < precision
12
14
  num, dec = get_hex_value(dec*16)
13
15
  second << num
14
16
  iterations += 1
@@ -58,7 +60,7 @@ class String
58
60
  end
59
61
 
60
62
  class Fixnum
61
- def to_hex
62
- self.to_f.to_hex
63
+ def to_hex(precision=HexValuesFromFloat::MAX_DECIMALS)
64
+ self.to_f.to_hex(precision)
63
65
  end
64
66
  end
@@ -39,6 +39,64 @@ describe 'HexValues' do
39
39
  end
40
40
  end
41
41
 
42
+ describe "float to hex with precision" do
43
+ it "converts 234.25 to EA.4 (no matter precision)" do
44
+ result = 234.25.to_hex(140)
45
+ result.must_equal "EA.4"
46
+ num, dec = result.split('.')
47
+ dec.size.must_equal 1
48
+ end
49
+
50
+ it "converts 1875.37 with 5 numbers after period" do
51
+ result = 1875.37.to_hex(5)
52
+ result.must_equal "753.5EB85"
53
+ num, dec = result.split('.')
54
+ dec.size.must_equal 5
55
+ end
56
+
57
+ it "converts 1875.37 with 50 numbers after period" do
58
+ result = 1875.37.to_hex(50)
59
+ num, dec = result.split('.')
60
+ dec.size.must_equal 50
61
+ end
62
+
63
+ it "converts 1875.37 with 0 numbers after period" do
64
+ result = 1875.37.to_hex(0)
65
+ result.must_equal "753"
66
+ num, dec = result.split('.')
67
+ dec.must_be_nil
68
+ end
69
+
70
+ it "converts 1875.37 with 14 numbers after period (if a negative precision is provided)" do
71
+ result = 1875.37.to_hex(-1)
72
+ result.must_equal "753.5EB851EB851EB8"
73
+ num, dec = result.split('.')
74
+ dec.size.must_equal 14
75
+ end
76
+ end
77
+
78
+ describe "integer to hex with precision" do
79
+ it "converts 1 to 1 (without parameter)" do
80
+ 1.to_hex.must_equal "1"
81
+ end
82
+
83
+ it "converts 1 to 1 (with precision = 40)" do
84
+ 1.to_hex(14).must_equal "1"
85
+ end
86
+
87
+ it "converts 1 to 1 (with precision = 0)" do
88
+ 1.to_hex(0).must_equal "1"
89
+ end
90
+
91
+ it "converts 1 to 1 (with precision = -1)" do
92
+ 1.to_hex(-1).must_equal "1"
93
+ end
94
+
95
+ it "converts 13 to D (with precision = 12)" do
96
+ 13.to_hex(12).must_equal "D"
97
+ end
98
+ end
99
+
42
100
  describe "hex to float" do
43
101
  it "converts EA.4 to 234.25" do
44
102
  "EA.4".to_float.must_equal 234.25
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hex_values
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: