-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsay_magnitudes.rb
89 lines (77 loc) · 2.08 KB
/
say_magnitudes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'pry'
class Magnitude
def self.say(num)
magnitudes = ["quadrillion","trillion","billion","million","thousand"]
num = num.to_s
said = ""
cut_at = -(magnitudes.length * 3.2).to_i
magnitudes.each do |mag|
section = num[0..cut_at]
section.gsub!(/\A0?0?0/, "")
said += "#{section} #{mag} " unless section == ""
num.slice! section
cut_at += 3
end
num.gsub!(/\A0?0?0/, "")
said += num
puts said
end
end
Magnitude.say(59999599002067890)
# class Magnitude
# def self.say(num)
# num = num.to_s
# said = ""
# if num.length <= 12 # 999 billion
# if num.length >= 10 # 1 billion
# said += "#{num[0..-10]} billion " unless num[0..-10] == "000"
# num.slice! num[0..-10]
# end
# if num.length >= 7 # 1 million
# said += "#{num[0..-7]} million " unless num[0..-7] == "000"
# num.slice! num[0..-7]
# end
# if num.length >= 4 # 1 thousand
# said += "#{num[0..-4]} thousand " unless num[0..-4] == "000"
# num.slice! num[0..-4]
# end
# said += num
# end
# puts said
# end
# end
# Magnitude.say(1010567890)
# class Magnitude
# def self.say(num)
# said = ""
# if num <= 999999999999 # 999 billion
# if num >= 1000000000
# num = num.to_s
# said += num[0..-10]
# said += " billion "
# num.slice! num[0..-10]
# said += num[0..-7] + " million "
# num.slice! num[0..-7]
# said += num[0..-4] + " thousand "
# num.slice! num[0..-4]
# said += num
# elsif num >= 1000000 # 1 million
# num = num.to_s
# said += num[0..-7] + " million "
# num.slice! num[0..-7]
# said += num[0..-4] + " thousand "
# num.slice! num[0..-4]
# said += num
# elsif num >= 1000 # 1 thousand
# num = num.to_s
# said += num[0..-4] + " thousand "
# num.slice! num[0..-4]
# said += num
# else
# said += num.to_s
# end
# end
# puts said
# end
# end
# Magnitude.say(4567890)