Created
July 25, 2016 07:13
-
-
Save artemkrachulov/233d447594f7dc5ec0291a7124fc17e6 to your computer and use it in GitHub Desktop.
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
// | |
// Double-Float+Rounding.swift | |
// Extension for Ceil, Floor and Round methods. | |
// | |
// Created by Artem Krachulov. | |
// Copyright (c) 2016 Artem Krachulov. All rights reserved. | |
// Website: http://www.artemkrachulov.com/ | |
// | |
import UIKit | |
extension Double { | |
/// Rounds the value to the nearest with precision count. | |
/// | |
/// Usage: | |
/// | |
/// 3.00.roundTo(2) // 3 | |
/// 3.035.roundTo(2) // 3.04 | |
/// 3.05.roundTo(1) // 3.1 | |
/// 3.999.roundTo(3) // 3.999 | |
/// | |
/// -3.00.ceilTo(2) // -3 | |
/// -3.035.ceilTo(2) // -3.04 | |
/// -3.05.ceilTo(1) // -3.1 | |
/// -3.999.ceilTo(3) // -3.999 | |
/// | |
public func roundTo(precision: Int) -> Double { | |
let divisor = pow(10.0, Double(precision)) | |
return round(self * divisor) / divisor | |
} | |
/// Rounds the value down to the next smaller whole number with precision count. | |
/// | |
/// Usage: | |
/// | |
/// 3.00.floorTo(2) // 3 | |
/// 3.035.floorTo(2) // 3.03 | |
/// 3.05.floorTo(1) // 3 | |
/// 3.999.floorTo(3) // 3.999 | |
/// | |
/// -3.00.ceilTo(2) // -3 | |
/// -3.035.ceilTo(2) // -3.04 | |
/// -3.05.ceilTo(1) // -3.1 | |
/// -3.999.ceilTo(3) // -3.999 | |
/// | |
public func floorTo(precision: Int) -> Double { | |
let divisor = pow(10.0, Double(precision)) | |
return floor(self * divisor) / divisor | |
} | |
/// Rounds the value up to the next larger whole number with precision count. | |
/// | |
/// Usage: | |
/// | |
/// 3.00.ceilTo(2) // 3 | |
/// 3.035.ceilTo(2) // 3.04 | |
/// 3.05.ceilTo(1) // 3.1 | |
/// 3.999.ceilTo(3) // 3.999 | |
/// | |
/// -3.00.ceilTo(2) // -3 | |
/// -3.035.ceilTo(2) // -3.03 | |
/// -3.05.ceilTo(1) // -3 | |
/// -3.999.ceilTo(3) // -3.999 | |
/// | |
public func ceilTo(precision: Int) -> Double { | |
let divisor = pow(10.0, Double(precision)) | |
return ceil(self * divisor) / divisor | |
} | |
} | |
/// Rounds the value to the nearest with multiplier. | |
/// | |
/// Usage: | |
/// | |
/// round(3.00, multiplier: 0.05) // 3 | |
/// round(3.035, multiplier: 0.01) // 3.04 | |
/// round(3.05, multiplier: 0.55) // 3.3 | |
/// round(3.999, multiplier: 1.25) // 3.75 | |
/// | |
/// round(-3.00, multiplier: 0.05) // -3 | |
/// round(-3.035, multiplier: 0.01) // -3.04 | |
/// round(-3.05, multiplier: 0.55) // -3.3 | |
/// round(-3.999, multiplier: 1.25) // -3.75 | |
/// | |
public func round(x: Double, multiplier: Double) -> Double { | |
return multiplier * round(x / multiplier) | |
} | |
/// Rounds the value down to the next smaller whole number with multiplier. | |
/// | |
/// Usage: | |
/// | |
/// floor(3.00, multiplier: 0.05) // 3 | |
/// floor(3.035, multiplier: 0.01) // 3.03 | |
/// floor(3.05, multiplier: 0.55) // 2.75 | |
/// floor(3.999, multiplier: 1.25) // 3.75 | |
/// | |
/// floor(-3.00, multiplier: 0.05) // -3 | |
/// floor(-3.035, multiplier: 0.01) // -3.04 | |
/// floor(-3.05, multiplier: 0.55) // -3.3 | |
/// floor(-3.999, multiplier: 1.25) // -5 | |
/// | |
public func floor(x: Double, multiplier: Double) -> Double { | |
return multiplier * floor(x / multiplier) | |
} | |
/// Rounds the value up to the next larger whole number with multiplier. | |
/// | |
/// Usage: | |
/// | |
/// ceil(3.00, multiplier: 0.05) // 3 | |
/// ceil(3.035, multiplier: 0.01) // 3.04 | |
/// ceil(3.05, multiplier: 0.55) // 3.3 | |
/// ceil(3.999, multiplier: 1.25) // 5 | |
/// | |
/// ceil(-3.00, multiplier: 0.05) // -3 | |
/// ceil(-3.035, multiplier: 0.01) // -3.03 | |
/// ceil(-3.05, multiplier: 0.55) // -2.75 | |
/// ceil(-3.999, multiplier: 1.25) // -3.75 | |
/// | |
public func ceil(x: Double, multiplier: Double) -> Double { | |
return multiplier * ceil(x / multiplier) | |
} | |
extension Float { | |
/// Rounds the value to the nearest with precision count. | |
/// | |
/// Usage: | |
/// | |
/// 3.00.roundTo(2) // 3 | |
/// 3.035.roundTo(2) // 3.04 | |
/// 3.05.roundTo(1) // 3.1 | |
/// 3.999.roundTo(3) // 3.999 | |
/// | |
/// -3.00.ceilTo(2) // -3 | |
/// -3.035.ceilTo(2) // -3.04 | |
/// -3.05.ceilTo(1) // -3.1 | |
/// -3.999.ceilTo(3) // -3.999 | |
/// | |
public func roundTo(precision: Int) -> Float { | |
let divisor = pow(10.0, Float(precision)) | |
return round(self * divisor) / divisor | |
} | |
/// Rounds the value down to the next smaller whole number with precision count. | |
/// | |
/// Usage: | |
/// | |
/// 3.00.floorTo(2) // 3 | |
/// 3.035.floorTo(2) // 3.03 | |
/// 3.05.floorTo(1) // 3 | |
/// 3.999.floorTo(3) // 3.999 | |
/// | |
/// -3.00.ceilTo(2) // -3 | |
/// -3.035.ceilTo(2) // -3.04 | |
/// -3.05.ceilTo(1) // -3.1 | |
/// -3.999.ceilTo(3) // -3.999 | |
/// | |
public func floorTo(precision: Int) -> Float { | |
let divisor = pow(10.0, Float(precision)) | |
return floor(self * divisor) / divisor | |
} | |
/// Rounds the value up to the next larger whole number with precision count. | |
/// | |
/// Usage: | |
/// | |
/// 3.00.ceilTo(2) // 3 | |
/// 3.035.ceilTo(2) // 3.04 | |
/// 3.05.ceilTo(1) // 3.1 | |
/// 3.999.ceilTo(3) // 3.999 | |
/// | |
/// -3.00.ceilTo(2) // -3 | |
/// -3.035.ceilTo(2) // -3.03 | |
/// -3.05.ceilTo(1) // -3 | |
/// -3.999.ceilTo(3) // -3.999 | |
/// | |
public func ceilTo(precision: Int) -> Float { | |
let divisor = pow(10.0, Float(precision)) | |
return ceil(self * divisor) / divisor | |
} | |
} | |
/// Rounds the value to the nearest with multiplier. | |
/// | |
/// Usage: | |
/// | |
/// round(3.00, multiplier: 0.05) // 3 | |
/// round(3.035, multiplier: 0.01) // 3.04 | |
/// round(3.05, multiplier: 0.55) // 3.3 | |
/// round(3.999, multiplier: 1.25) // 3.75 | |
/// | |
/// round(-3.00, multiplier: 0.05) // -3 | |
/// round(-3.035, multiplier: 0.01) // -3.04 | |
/// round(-3.05, multiplier: 0.55) // -3.3 | |
/// round(-3.999, multiplier: 1.25) // -3.75 | |
/// | |
public func round(x: Float, multiplier: Float) -> Float { | |
return multiplier * round(x / multiplier) | |
} | |
/// Rounds the value down to the next smaller whole number with multiplier. | |
/// | |
/// Usage: | |
/// | |
/// floor(3.00, multiplier: 0.05) // 3 | |
/// floor(3.035, multiplier: 0.01) // 3.03 | |
/// floor(3.05, multiplier: 0.55) // 2.75 | |
/// floor(3.999, multiplier: 1.25) // 3.75 | |
/// | |
/// floor(-3.00, multiplier: 0.05) // -3 | |
/// floor(-3.035, multiplier: 0.01) // -3.04 | |
/// floor(-3.05, multiplier: 0.55) // -3.3 | |
/// floor(-3.999, multiplier: 1.25) // -5 | |
/// | |
public func floor(x: Float, multiplier: Float) -> Float { | |
return multiplier * floor(x / multiplier) | |
} | |
/// Rounds the value up to the next larger whole number with multiplier. | |
/// | |
/// Usage: | |
/// | |
/// ceil(3.00, multiplier: 0.05) // 3 | |
/// ceil(3.035, multiplier: 0.01) // 3.04 | |
/// ceil(3.05, multiplier: 0.55) // 3.3 | |
/// ceil(3.999, multiplier: 1.25) // 5 | |
/// | |
/// ceil(-3.00, multiplier: 0.05) // -3 | |
/// ceil(-3.035, multiplier: 0.01) // -3.03 | |
/// ceil(-3.05, multiplier: 0.55) // -2.75 | |
/// ceil(-3.999, multiplier: 1.25) // -3.75 | |
/// | |
public func ceil(x: Float, multiplier: Float) -> Float { | |
return multiplier * ceil(x / multiplier) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Functions like
roundTo(precision: Int) -> Double
don't compile anymore using Swift 4...I had to change it to: