Last active
April 16, 2020 15:20
-
-
Save abdullahbutt/d22298399d771e10ae134aa013ebe8f4 to your computer and use it in GitHub Desktop.
swift bmi calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Playground - noun: a place where people can play | |
| /* | |
| The body mass index (BMI) is a measure used to quantify a person’s mass as well as interpret their body composition. It is defined as the mass (Kg) divided by height (m) squared. | |
| Here’s the BMI calculation formula: | |
| BMI = mass (kg) / height (m2) // Metre square | |
| If the BMI is greater than 25, use the print statement to tell the user that they are overweight. | |
| Otherwise, if the BMI is between 18.5 - 25, tell the user that they are of normal weight. | |
| Finally, if their BMI is below 18.5, tell the user that they are underweight. | |
| ---------------------------------- Difference Between Float & Double ------------------------------- | |
| let floatingPointNumber : Float = 1.3 // max Float length after decimal can be 6 | |
| let double : Double = 3.14159265359 // Double are 64 bit numbers.max Double length after decimal can be 15 | |
| ---------------------------------------------------------------------------------------------------- | |
| */ | |
| import UIKit | |
| func calcBMI (mass : Double, height : Double) -> String{ | |
| //let myBmi = mass / (height * height) | |
| let bmi = mass / pow(height, 2) | |
| //let shortenedBmi = round(bmi) // Not good. It is celing function & makes 29.7299 to 30.0 | |
| let shortenedBmi = String(format: "%.2f", bmi) // It rounds to 2 | |
| //let shortenedBmi = String(format: "%1.0f", bmi) // It rounds to 0 | |
| //let shortenedBmi = String(format: "%1.1f", bmi) // It rounds to 1 | |
| var interpretation = "" | |
| if bmi > 25{ | |
| interpretation = "You are overweight mate." | |
| } | |
| //else if myBmi >= 18.5 && myBmi <= 25{ | |
| else if bmi >= 18.5{ | |
| interpretation = "You are healthy mate." | |
| } | |
| else | |
| { | |
| interpretation = "You are underweight mate." | |
| } | |
| //return "Your BMI is \(bmi). " + interpretation + " & BMI of a healthy guy is between 18.5 & 25" | |
| //return "Your BMI is " + String(bmi) + "." + interpretation + " & BMI of a healthy guy is between 18.5 & 25" // String(bmi) converts bmi data Type to String (Type Casting) | |
| return "Your BMI is \(shortenedBmi). \(interpretation) & BMI of a healthy guy is between 18.5 & 25" | |
| } | |
| print(calcBMI(mass: 90, height: 1.7399)) | |
| /* | |
| -----------------------------BMI Calculate Imperial Units-------------------------------- | |
| // 1 foot = 12 inches | |
| // 1 inch = 0.0254 meters | |
| // 1 pound = 0.45359237 kilograms | |
| // Example a person weighs 140 pounds & height is 5 ft 11 inches | |
| ----------------------------------------------------------------------------------------- | |
| */ | |
| func bmiCalcImperialUnits(weightInPounds : Double, heightInFeet: Double, remainderInches: Double) -> String | |
| { | |
| let weightInKg = weightInPounds * 0.45359237 | |
| let totalInches = (heightInFeet * 12) + remainderInches | |
| let heightInMeters = totalInches * 0.0254 | |
| let bmi = weightInKg / pow(heightInMeters, 2) | |
| let shortenedBmi = String(format: "%.2f", bmi) | |
| return shortenedBmi | |
| } | |
| print(bmiCalcImperialUnits(weightInPounds: 140, heightInFeet: 5, remainderInches: 11)) |
Hi, I have made another method using your code according to my requirement. What it will do is, it will take the below input from user:
unit of weight (st/lbs OR kg/gram)
unit of height (ft/inches OR cm/mm)
and for every weight you will have to provide 2 input values, like feet and inches, kg and grams, stone and pounds, cm and mm.
if the user don't want to provide the input for the second value, then just provide 0.
func bmiCalcImperialUnits(weightBeforeDecimal : Double, weightAfterDecimal: Double, heightBeforeDecimal: Double, heightAfterDecimal: Double, heightUnit: String , weightUnit:String) -> String
{
var weightInKg = 0.0
var totalInches = 0.0
var heightInMeters = 0.0
if heightUnit == "cm" && weightUnit == "kg"
{
let cm = heightBeforeDecimal
let mm = heightAfterDecimal
let totalCMs = cm + (mm/10)
// let feet = cm / 30.48
// let inches = mm/25.4
totalInches = totalCMs/2.54
heightInMeters = totalInches * 0.0254
let kg = weightBeforeDecimal
let gram = weightAfterDecimal
let totalKGs = kg + (gram / 1000)
weightInKg = totalKGs
}
else if heightUnit == "cm" && weightUnit == "st/lbs"
{
let cm = heightBeforeDecimal
let mm = heightAfterDecimal
let totalCMs = cm + (mm/10)
// let feet = cm / 30.48
// let inches = mm/25.4
totalInches = totalCMs/2.54
heightInMeters = totalInches * 0.0254
let stone = weightBeforeDecimal
let pound = weightAfterDecimal
let totalStones = stone + (pound / 14)
weightInKg = totalStones * 6.35
}
else if heightUnit == "ft/inches" && weightUnit == "kg"
{
let heightInFeet = heightBeforeDecimal
let remainderInches = heightAfterDecimal
let totalInches = (heightInFeet * 12) + remainderInches
heightInMeters = totalInches * 0.0254
let kg = weightBeforeDecimal
let gram = weightAfterDecimal
let totalKGs = kg + (gram / 1000)
weightInKg = totalKGs
}
else if heightUnit == "ft/inches" && weightUnit == "st/lbs"
{
let heightInFeet = heightBeforeDecimal
let remainderInches = heightAfterDecimal
let totalInches = (heightInFeet * 12) + remainderInches
heightInMeters = totalInches * 0.0254
let stone = weightBeforeDecimal
let pound = weightAfterDecimal
let totalStones = stone + (pound / 14)
weightInKg = totalStones * 6.35
}
let bmi = weightInKg / pow(heightInMeters, 2)
let shortenedBmi = String(format: "%.2f", bmi)
return shortenedBmi
}
and how to call the method
print("BMI 1 cm kg : \(bmiCalcImperialUnits(weightBeforeDecimal: 60, weightAfterDecimal: 999, heightBeforeDecimal: 100, heightAfterDecimal: 5, heightUnit: "cm", weightUnit: "kg"))")
print("BMI 2 cm st/lbs : \(bmiCalcImperialUnits(weightBeforeDecimal: 5, weightAfterDecimal: 13, heightBeforeDecimal: 100, heightAfterDecimal: 5, heightUnit: "cm", weightUnit: "st/lbs"))")
print("BMI 3 ft/inches kg : \(bmiCalcImperialUnits(weightBeforeDecimal: 60, weightAfterDecimal: 999, heightBeforeDecimal: 5, heightAfterDecimal: 11, heightUnit: "ft/inches", weightUnit: "kg"))")
print("BMI 4 ft/inches st/lbs : \(bmiCalcImperialUnits(weightBeforeDecimal: 5, weightAfterDecimal: 13, heightBeforeDecimal: 5, heightAfterDecimal: 11, heightUnit: "ft/inches", weightUnit: "st/lbs"))")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I just randomly came across your gist and used it as an exercise to rewrite it using enums. I figured I might as well share it with you :).