Skip to content

Instantly share code, notes, and snippets.

@junyng
Created April 2, 2019 05:09
Show Gist options
  • Select an option

  • Save junyng/0a2b6edf4a4d2e16fc6107659f33fb08 to your computer and use it in GitHub Desktop.

Select an option

Save junyng/0a2b6edf4a4d2e16fc6107659f33fb08 to your computer and use it in GitHub Desktop.
PairProgramming
//
// main.swift
// PairProgramming
//
// Created by Blu X Bean on 2019. 4. 2..
// Copyright © 2019년 Blu Bean. All rights reserved.
//
import Foundation
//print("\(120)cm to \(120 * 0.01)m")
//print("\(1.86)m to \(Int(1.86 * 100))cm")
func convert(value: String) {
let number: Double = 100
var units = value.split(separator: " ")
if units.count == 1 {
if value.contains("cm") {
if let convertedValue = Double(value.replacingOccurrences(of: "cm", with: "", options: .literal, range: nil)) {
print("\(convertedValue / number)m")
}
} else if value.contains("m"){
if let convertedValue = Double(value.replacingOccurrences(of: "m", with: "", options: .literal, range: nil)) {
print("\(Int(convertedValue * number))cm")
}
} else if value.contains("inch"){
if let convertedValue = Double(value.replacingOccurrences(of: "inch", with: "", options: .literal, range: nil)) {
print("\(convertedValue * 2.54)cm")
}
}
} else if units.count == 2 {
switch units[1] {
case "cm" : break
case "inch" : break
case "m" : break
default: break
}
}
}
//let input = readLine()
//convert(value: input ?? "")
var s = "8inch"
convert(value: s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment