BFFloat+Ext.swift 805 B

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // BFFloat+Ext.swift
  3. // BFFramework
  4. //
  5. // Created by ak on 2021/10/11.
  6. //
  7. import Foundation
  8. extension Float {
  9. /// 准确的小数尾截取 - 没有进位
  10. /*
  11. // 11.999003 -> 12.0
  12. var pp = 11.999003
  13. String(format: "%.1f", pp) 这个方法会进行四舍五入
  14. */
  15. func decimalString(_ base: Self = 1) -> String {
  16. return "\(self.decimalNumber(base))"
  17. }
  18. func decimalNumber(_ base: Self = 1) -> Float {
  19. let tempCount: Self = pow(10, base)
  20. let temp = self*tempCount
  21. let target = Self(Int(temp))
  22. let stepone = target/tempCount
  23. if stepone.truncatingRemainder(dividingBy: 1) == 0 {
  24. return Float(String(format: "%.0f", stepone)) ?? 0.0
  25. }else{
  26. return stepone
  27. }
  28. }
  29. }