mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-14 08:01:05 +01:00
Merge pull request #136 from msaf9/master
weight conversions feature implementation
This commit is contained in:
commit
9b5a1565be
2 changed files with 107 additions and 0 deletions
|
@ -10,6 +10,7 @@
|
|||
|
||||
## Conversions
|
||||
* [Temperature Conversions](https://github.com/TheAlgorithms/Ruby/blob/master/conversions/temperature_conversions.rb)
|
||||
* [Weight Conversions](https://github.com/TheAlgorithms/Ruby/blob/master/conversions/weight_conversions.rb)
|
||||
|
||||
## Data Structures
|
||||
* Arrays
|
||||
|
|
106
conversions/weight_conversions.rb
Normal file
106
conversions/weight_conversions.rb
Normal file
|
@ -0,0 +1,106 @@
|
|||
# A ruby program for weight conversions
|
||||
|
||||
module WeightConversion
|
||||
# Kilogram -> Gram = (kilogram_value * 1000) grams
|
||||
def self.kilogram_to_gram(kilogram_input)
|
||||
raise StandardError unless Integer === kilogram_input
|
||||
|
||||
gram = kilogram_input * 1000
|
||||
|
||||
"#{kilogram_input} kg = #{gram} g"
|
||||
end
|
||||
|
||||
# Gram -> Kilogram = (gram_value / 1000) kilograms
|
||||
def self.gram_to_kilogram(gram_input)
|
||||
kilogram = gram_input / 1000
|
||||
|
||||
"#{gram_input} g = #{kilogram} kg"
|
||||
end
|
||||
|
||||
# Pound -> Ounce = (pound_value * 16) oz
|
||||
def self.pound_to_ounce(pound_input)
|
||||
ounce = pound_input * 16
|
||||
|
||||
"#{pound_input} lb = #{ounce} oz"
|
||||
end
|
||||
|
||||
# Ounce -> Pound = (ounce_value / 16) lb
|
||||
def self.ounce_to_pound(ounce_input)
|
||||
pound = ounce_input / 16
|
||||
|
||||
"#{ounce_input} oz = #{pound} lb"
|
||||
end
|
||||
|
||||
# Kilogram -> Pound = (kilogram_input * 2.205) lb
|
||||
def self.kilogram_to_pound(kilogram_input)
|
||||
pound = (kilogram_input * 2.205).round(2)
|
||||
|
||||
"#{kilogram_input} kg = #{pound} lb"
|
||||
end
|
||||
|
||||
# Pound -> Kilogram = (pound_input / 2.205) kg
|
||||
def self.pound_to_kilogram(pound_input)
|
||||
raise StandardError unless Integer === pound_input
|
||||
|
||||
kilogram = (pound_input / 2.205).round(2)
|
||||
|
||||
"#{pound_input} lb = #{kilogram} kg"
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# Valid inputs
|
||||
#
|
||||
|
||||
puts WeightConversion.kilogram_to_gram(2)
|
||||
# 2 kg = 2000 g
|
||||
puts WeightConversion.gram_to_kilogram(3000)
|
||||
# 3000 g = 3 kg
|
||||
puts WeightConversion.pound_to_ounce(16)
|
||||
# 16 lb = 256 oz
|
||||
puts WeightConversion.ounce_to_pound(16)
|
||||
# 16 oz = 1 lb
|
||||
puts WeightConversion.kilogram_to_pound(1)
|
||||
# 1 kg = 2.21 lb
|
||||
puts WeightConversion.pound_to_kilogram(100)
|
||||
# 100 lb = 45.35 kg
|
||||
|
||||
#
|
||||
# Invalid inputs
|
||||
#
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("a")
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
end
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("3000")
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
end
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("16")
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
end
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("x ")
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
end
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("weight")
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
end
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("100")
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
end
|
Loading…
Reference in a new issue