Skip to content

Commit 9667ec3

Browse files
committed
fix formatted display
1 parent 4a496f6 commit 9667ec3

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

lib/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def metric_prefixes
1515
# @see #metric_prefixes
1616
# @return [Array<Number>] An array of metric powers that correspont to each metric prefix
1717
def metric_power
18-
metric_prefixes.map.with_index { |_item, index| (1000**(index + 1)) }
18+
metric_prefixes.map.with_index { |_item, index| (1000**(index + 1)).to_i }
1919
end
2020

2121
# Method that is used to parse a string as JSON , if it fails will return nil

lib/number_formatter.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# @return [Number] The number that will be formatted in different format
77
class NumberFormatter
88
include Helper
9-
attr_reader :number
9+
attr_reader :number, :text, :display_metric
1010

1111
# Method used for instantiating the NumberFormatter with the number that will be used
1212
# and a boolean value that will decide if we format in metric format or using delimiters
@@ -18,14 +18,17 @@ class NumberFormatter
1818
def initialize(number, display_metric)
1919
@number = number.present? ? number : 0
2020
@display_metric = display_metric
21-
formatted_display
21+
if @number.to_s =~ /(\d+)(.*)/m
22+
@number = Regexp.last_match(1).to_i
23+
@text = Regexp.last_match(2)
24+
end
2225
end
2326

2427
# Returns the number as a string
2528
#
2629
# @return [String] Returns the number as a string
2730
def to_s
28-
@number.to_s
31+
formatted_display
2932
end
3033

3134
# Method that is used to decide which format to use depending on the instance
@@ -47,11 +50,10 @@ def number_with_metric
4750
while index >= 0
4851
limit = metric_power[index]
4952
if @number > limit
50-
return "#{((@number / limit).to_f.round)}#{metric_prefixes[index]}"
53+
return "#{((@number / limit).to_f.round)}#{metric_prefixes[index]}#{@text}"
5154
end
5255
index -= 1
5356
end
54-
@number.to_s
5557
end
5658

5759
# Description of method
@@ -62,8 +64,8 @@ def number_with_metric
6264
def number_with_delimiter(delimiter = ',', separator = '.')
6365
parts = @number.to_s.split('.')
6466
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
65-
parts.join separator
67+
"#{parts.join separator}#{@text}".to_s
6668
rescue
67-
@number
69+
@number.to_s
6870
end
6971
end

0 commit comments

Comments
 (0)