StringUtil.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //
  2. // StringUtil.swift
  3. // HCQuanfangtong
  4. //
  5. // Created by Apple on 2021/12/24.
  6. // Copyright © 2021 Jyp. All rights reserved.
  7. //
  8. import Foundation
  9. //MARK: String 运算符重载
  10. infix operator ++ : AdditionPrecedence
  11. infix operator -- : AdditionPrecedence
  12. infix operator ** : MultiplicationPrecedence
  13. infix operator / : MultiplicationPrecedence
  14. infix operator < : AdditionPrecedence
  15. infix operator <= : AdditionPrecedence
  16. infix operator > : AdditionPrecedence
  17. infix operator >= : AdditionPrecedence
  18. infix operator === : AdditionPrecedence
  19. infix operator != : AdditionPrecedence
  20. //不控制小数位 精细计算
  21. infix operator ++! : AdditionPrecedence
  22. infix operator --! : AdditionPrecedence
  23. infix operator **! : MultiplicationPrecedence
  24. infix operator /! : MultiplicationPrecedence
  25. //MARK: 💰 格式化
  26. extension String{
  27. static func moneyFormatter(_ value : String?) -> String {
  28. if value == nil{
  29. return "0.00"
  30. }
  31. let result = Double(value!)
  32. if result?.isNaN ?? true{
  33. return "0.00"
  34. }
  35. return String.init(format: "%.2f", (result ?? 0))
  36. }
  37. static func moneyFormatter(_ value : Double?) -> String {
  38. if value == nil{
  39. return "0.00"
  40. }
  41. if value?.isNaN ?? true{
  42. return "0.00"
  43. }
  44. return String.init(format: "%.2f", value!)
  45. }
  46. /// 不控制小数位
  47. /// - Parameter value: <#value description#>
  48. /// - Returns: <#description#>
  49. static func moneyFormatterNotKeepTwoDecimal(_ value : String?) -> String{
  50. if value == nil{
  51. return "0.00"
  52. }
  53. let result = Double(value!)
  54. if result?.isNaN ?? true{
  55. return "0.00"
  56. }
  57. return String.init(format: "%f", (result ?? 0))
  58. }
  59. static func moneyFormatterNotKeepTwoDecimal(_ value : Double?) -> String{
  60. if value == nil{
  61. return "0.00"
  62. }
  63. if value?.isNaN ?? true{
  64. return "0.00"
  65. }
  66. return String.init(format: "%f", value!)
  67. }
  68. }
  69. func ++(str:String?, str1:String?) -> String{
  70. guard str == nil && str1 == nil else {
  71. guard let _ = str else {
  72. return String.moneyFormatter(str1)
  73. }
  74. guard let _ = str1 else {
  75. return String.moneyFormatter(str)
  76. }
  77. let number = NSDecimalNumber.init(string: str)
  78. let number1 = NSDecimalNumber.init(string: str1)
  79. if number.decimalValue.isNaN{
  80. return String.moneyFormatter(number1.stringValue)
  81. }
  82. if number1.decimalValue.isNaN{
  83. return String.moneyFormatter(number.stringValue)
  84. }
  85. return String.moneyFormatter(number.adding(number1).stringValue)
  86. }
  87. return "0.00"
  88. }
  89. func ++!(str:String?, str1:String?) -> String{
  90. guard str == nil && str1 == nil else {
  91. guard let _ = str else {
  92. return String.moneyFormatterNotKeepTwoDecimal(str1)
  93. }
  94. guard let _ = str1 else {
  95. return String.moneyFormatterNotKeepTwoDecimal(str)
  96. }
  97. let number = NSDecimalNumber.init(string: str)
  98. let number1 = NSDecimalNumber.init(string: str1)
  99. if number.decimalValue.isNaN{
  100. return String.moneyFormatterNotKeepTwoDecimal(number1.stringValue)
  101. }
  102. if number1.decimalValue.isNaN{
  103. return String.moneyFormatterNotKeepTwoDecimal(number.stringValue)
  104. }
  105. return String.moneyFormatterNotKeepTwoDecimal(number.adding(number1).stringValue)
  106. }
  107. return "0.00"
  108. }
  109. func --(str:String?, str1:String?) -> String{
  110. guard str == nil && str1 == nil else {
  111. guard let _ = str else {
  112. return "-" + String.moneyFormatter(str1)
  113. }
  114. guard let _ = str1 else {
  115. return String.moneyFormatter(str)
  116. }
  117. let number = NSDecimalNumber.init(string: str)
  118. let number1 = NSDecimalNumber.init(string: str1)
  119. if number1.decimalValue.isNaN{
  120. return String.moneyFormatter(number.stringValue)
  121. }
  122. if number.decimalValue.isNaN{
  123. if number.decimalValue.isZero{
  124. return "0.00"
  125. }
  126. return "-" + String.moneyFormatter(number1.stringValue)
  127. }
  128. return String.moneyFormatter(number.subtracting(number1).stringValue)
  129. }
  130. return "0.00"
  131. }
  132. func --!(str:String?, str1:String?) -> String{
  133. guard str == nil && str1 == nil else {
  134. guard let _ = str else {
  135. return "-" + String.moneyFormatterNotKeepTwoDecimal(str1)
  136. }
  137. guard let _ = str1 else {
  138. return String.moneyFormatterNotKeepTwoDecimal(str)
  139. }
  140. let number = NSDecimalNumber.init(string: str)
  141. let number1 = NSDecimalNumber.init(string: str1)
  142. if number1.decimalValue.isNaN{
  143. return String.moneyFormatterNotKeepTwoDecimal(number.stringValue)
  144. }
  145. if number.decimalValue.isNaN{
  146. if number.decimalValue.isZero{
  147. return "0.00"
  148. }
  149. return "-" + String.moneyFormatterNotKeepTwoDecimal(number1.stringValue)
  150. }
  151. return String.moneyFormatterNotKeepTwoDecimal(number.subtracting(number1).stringValue)
  152. }
  153. return "0.00"
  154. }
  155. func **(str:String?, str1:String?) -> String{
  156. guard str == nil || str1 == nil else {
  157. let number = NSDecimalNumber.init(string: str)
  158. let number1 = NSDecimalNumber.init(string: str1)
  159. if number.decimalValue.isNaN || number1.decimalValue.isNaN{
  160. return "0.00"
  161. }
  162. return String.moneyFormatter(number.multiplying(by:number1).stringValue)
  163. }
  164. return "0.00"
  165. }
  166. func **!(str:String?, str1:String?) -> String{
  167. guard str == nil || str1 == nil else {
  168. let number = NSDecimalNumber.init(string: str)
  169. let number1 = NSDecimalNumber.init(string: str1)
  170. if number.decimalValue.isNaN || number1.decimalValue.isNaN{
  171. return "0.00"
  172. }
  173. return String.moneyFormatterNotKeepTwoDecimal(number.multiplying(by:number1).stringValue)
  174. }
  175. return "0.00"
  176. }
  177. func /(str:String?, str1:String?) -> String{
  178. if str1 == nil{
  179. return "0.00"
  180. }
  181. let number1 = NSDecimalNumber.init(string: str1)
  182. let number = NSDecimalNumber.init(string: str)
  183. if number.decimalValue.isNaN || number1.decimalValue.isNaN{
  184. return "0.00"
  185. }
  186. if number1.decimalValue.isZero{
  187. return "0.00"
  188. }
  189. return String.moneyFormatter(number.dividing(by:number1).stringValue)
  190. }
  191. func /!(str:String?, str1:String?) -> String{
  192. if str1 == nil{
  193. return "0.00"
  194. }
  195. let number1 = NSDecimalNumber.init(string: str1)
  196. let number = NSDecimalNumber.init(string: str)
  197. if number.decimalValue.isNaN || number1.decimalValue.isNaN{
  198. return "0.00"
  199. }
  200. if number1.decimalValue.isZero{
  201. return "0.00"
  202. }
  203. return String.moneyFormatterNotKeepTwoDecimal(number.dividing(by:number1).stringValue)
  204. }
  205. func <(str:String?,number:Any) -> Bool{
  206. let firstStr = NSDecimalNumber.init(string: str++"")
  207. if number is Int{
  208. if firstStr.compare(NSNumber.init(value: number as! Int)) == ComparisonResult.orderedAscending{
  209. return true
  210. }
  211. }
  212. else if number is Double{
  213. if firstStr.compare(NSNumber.init(value: number as! Double)) == ComparisonResult.orderedAscending{
  214. return true
  215. }
  216. }
  217. else if number is Float{
  218. if firstStr.compare(NSNumber.init(value: number as! Float)) == ComparisonResult.orderedAscending{
  219. return true
  220. }
  221. }
  222. return false
  223. }
  224. func <=(str:String?,number:Any)->Bool{
  225. let firstStr = NSDecimalNumber.init(string: str++"")
  226. if number is Int{
  227. if firstStr.compare(NSNumber.init(value: number as! Int)) != ComparisonResult.orderedDescending{
  228. return true
  229. }
  230. }
  231. else if number is Double{
  232. if firstStr.compare(NSNumber.init(value: number as! Double)) != ComparisonResult.orderedDescending{
  233. return true
  234. }
  235. }
  236. else if number is Float{
  237. if firstStr.compare(NSNumber.init(value: number as! Float)) != ComparisonResult.orderedDescending{
  238. return true
  239. }
  240. }
  241. return false
  242. }
  243. func >(str:String?,number:Any) -> Bool{
  244. let firstStr = NSDecimalNumber.init(string: str++"")
  245. if number is Int{
  246. if firstStr.compare(NSNumber.init(value: number as! Int)) == ComparisonResult.orderedDescending{
  247. return true
  248. }
  249. }
  250. else if number is Double{
  251. if firstStr.compare(NSNumber.init(value: number as! Double)) == ComparisonResult.orderedDescending{
  252. return true
  253. }
  254. }
  255. else if number is Float{
  256. if firstStr.compare(NSNumber.init(value: number as! Float)) == ComparisonResult.orderedDescending{
  257. return true
  258. }
  259. }
  260. return false
  261. }
  262. func >=(str:String?,number:Any)->Bool{
  263. let firstStr = NSDecimalNumber.init(string: str++"")
  264. if number is Int{
  265. if firstStr.compare(NSNumber.init(value: number as! Int)) != ComparisonResult.orderedAscending{
  266. return true
  267. }
  268. }
  269. else if number is Double{
  270. if firstStr.compare(NSNumber.init(value: number as! Double)) != ComparisonResult.orderedAscending{
  271. return true
  272. }
  273. }
  274. else if number is Float{
  275. if firstStr.compare(NSNumber.init(value: number as! Float)) != ComparisonResult.orderedAscending{
  276. return true
  277. }
  278. }
  279. return false
  280. }
  281. func === (str:String?,number:Any)->Bool{
  282. let firstStr = NSDecimalNumber.init(string: str++"")
  283. if number is Int{
  284. if firstStr.compare(NSNumber.init(value: number as! Int)) == ComparisonResult.orderedSame{
  285. return true
  286. }
  287. }
  288. else if number is Double{
  289. if firstStr.compare(NSNumber.init(value: number as! Double)) == ComparisonResult.orderedSame{
  290. return true
  291. }
  292. }
  293. else if number is Float{
  294. if firstStr.compare(NSNumber.init(value: number as! Float)) == ComparisonResult.orderedSame{
  295. return true
  296. }
  297. }
  298. return false
  299. }
  300. func != (str:String?,number:Any)->Bool{
  301. let firstStr = NSDecimalNumber.init(string: str++"")
  302. if number is Int{
  303. if firstStr.compare(NSNumber.init(value: number as! Int)) != ComparisonResult.orderedSame{
  304. return true
  305. }
  306. }
  307. else if number is Double{
  308. if firstStr.compare(NSNumber.init(value: number as! Double)) != ComparisonResult.orderedSame{
  309. return true
  310. }
  311. }
  312. else if number is Float{
  313. if firstStr.compare(NSNumber.init(value: number as! Float)) != ComparisonResult.orderedSame{
  314. return true
  315. }
  316. }
  317. return false
  318. }