BFNumber+Ext.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // BFNumber+Ext.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2020/7/20.
  6. // Copyright © 2020 BytesFlow. All rights reserved.
  7. //
  8. import Foundation
  9. public extension Int {
  10. /// 分数字格式化如413200->4,132.09
  11. /// 是否保留2位小数
  12. /// - Parameter isDecimal: <#isDecimal description#>
  13. /// - Returns: <#description#>
  14. func paraseDecimalFormatterValue(isDecimal: Bool = false) -> String? {
  15. let decimal = self % 100
  16. let nonDecimal = self / 100
  17. let formatter = NumberFormatter()
  18. formatter.numberStyle = .decimal
  19. if let title = formatter.string(from: NSNumber(value: nonDecimal)) {
  20. if isDecimal {
  21. return title + String(format: ".%02d", decimal)
  22. } else {
  23. return title
  24. }
  25. }
  26. return nil
  27. }
  28. /// 改变单位为万
  29. /// @param originUnit <#originUnit description#>
  30. func changeUnit() -> String {
  31. var unitStr: String = ""
  32. if self < 10000 {
  33. unitStr = "\(self)"
  34. } else if self <= 1_000_000 {
  35. let divisor = pow(10.0, Double(1))
  36. let decimal = ((Double(self) / 10000) * divisor).rounded() / divisor
  37. unitStr = "\(decimal)万"
  38. } else {
  39. unitStr = "\(self / 10000)万"
  40. }
  41. return unitStr
  42. }
  43. }
  44. // MARK: - Float64 double类型扩展
  45. /// Float64 double类型扩展
  46. public extension Float64 {
  47. /// 时长转化为分秒 62'52"
  48. /// - Returns: <#description#>
  49. func formatDurationToMS() -> String {
  50. let duration = lround(self)
  51. var text = ""
  52. let min = duration / 60
  53. let second = duration % 60
  54. if min > 0, second > 0 {
  55. text = "\(min)" + "\'" + "\(second)" + "\""
  56. } else if min > 0 {
  57. text = "\(min)" + "\'" + "0\""
  58. } else {
  59. text = "\(second)" + "\""
  60. }
  61. return text
  62. }
  63. /// 时长转化成时分秒 01:02:52
  64. /// - Parameter value: <#value description#>
  65. /// - Returns: <#description#>
  66. func formatDurationToHMS() -> String {
  67. var theTime = lround(self)
  68. var theTime1 = 0 // 分
  69. var theTime2 = 0 // 小时
  70. if theTime < 60 {
  71. if theTime < 10 {
  72. return "00:0\(theTime)"
  73. }
  74. return "00:\(theTime)"
  75. }
  76. theTime1 = theTime / 60
  77. theTime = theTime % 60
  78. if theTime1 > 60 {
  79. theTime2 = theTime1 / 60
  80. theTime1 = theTime1 % 60
  81. }
  82. var result = "\(theTime)"
  83. if theTime < 10 {
  84. result = "0" + result
  85. }
  86. if theTime1 > 0 {
  87. result = "\(theTime1):" + result
  88. if theTime1 < 10 {
  89. result = "0" + result
  90. }
  91. }
  92. if theTime2 > 0 {
  93. result = "\(theTime2):" + result
  94. if theTime2 < 10 {
  95. result = "0" + result
  96. }
  97. }
  98. return result
  99. }
  100. }
  101. extension Float {
  102. /// 准确的小数尾截取 - 没有进位
  103. /*
  104. // 11.999003 -> 12.0
  105. var pp = 11.999003
  106. String(format: "%.1f", pp) 这个方法会进行四舍五入
  107. */
  108. public func decimalString(_ base: Self = 1) -> String {
  109. return "\(self.decimalNumber(base))"
  110. }
  111. public func decimalNumber(_ base: Self = 1) -> Float {
  112. let tempCount: Self = pow(10, base)
  113. let temp = self*tempCount
  114. let target = Self(Int(temp))
  115. let stepone = target/tempCount
  116. if stepone.truncatingRemainder(dividingBy: 1) == 0 {
  117. return Float(String(format: "%.0f", stepone)) ?? 0.0
  118. }else{
  119. return stepone
  120. }
  121. }
  122. }