Hackerrank Day 2: Operators

Given the mealCost, tipPercent (the percentage of the meal price being added as tip), and taxPercent (the percentage of the meal price being added as tax) for a meal, find and print the meal’s totalCost.

My solution in Ruby:

# gets user input and convert them to float

mealCost = gets.to_f
tipPercent = gets.to_f
taxPercent = gets.to_f

tip = mealCost * (tipPercent / 100.0)
tax = mealCost * (taxPercent / 100.0)

totalCost = (mealCost + tip + tax).round

puts “The total meal cost is #{totalCost} dollars.”

I made one seemingly minor but critical mistake when I first solved the problem.  I divided tipPercent and tax Percent by 100, instead of 100.0.  This does not yield the same result in codes because when the computer divides by an integer (whole number) and not a float (decimals), it rounds the result to the nearest integer (which in this case, 20/100 = 0).  To get a float as your results, you have to change the values in the math operation to floats as well.