swift 双浮子+ Rounding.swift

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 双浮子+ Rounding.swift相关的知识,希望对你有一定的参考价值。

//
//  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)
}

以上是关于swift 双浮子+ Rounding.swift的主要内容,如果未能解决你的问题,请参考以下文章

css css - 用于包装里面的浮子

JavaScript 圆形浮子

CSS 清除浮子的最佳方法

清除浮子容器

逃走的浮子虫

css ClearFix CSS。在“浮子元件”的容器上强制高度。