// // StringUtil.swift // HCQuanfangtong // // Created by Apple on 2021/12/24. // Copyright © 2021 Jyp. All rights reserved. // import Foundation //MARK: String 运算符重载 infix operator ++ : AdditionPrecedence infix operator -- : AdditionPrecedence infix operator ** : MultiplicationPrecedence infix operator / : MultiplicationPrecedence infix operator < : AdditionPrecedence infix operator <= : AdditionPrecedence infix operator > : AdditionPrecedence infix operator >= : AdditionPrecedence infix operator === : AdditionPrecedence infix operator != : AdditionPrecedence //不控制小数位 精细计算 infix operator ++! : AdditionPrecedence infix operator --! : AdditionPrecedence infix operator **! : MultiplicationPrecedence infix operator /! : MultiplicationPrecedence //MARK: 💰 格式化 extension String{ static func moneyFormatter(_ value : String?) -> String { if value == nil{ return "0.00" } let result = Double(value!) if result?.isNaN ?? true{ return "0.00" } return String.init(format: "%.2f", (result ?? 0)) } static func moneyFormatter(_ value : Double?) -> String { if value == nil{ return "0.00" } if value?.isNaN ?? true{ return "0.00" } return String.init(format: "%.2f", value!) } /// 不控制小数位 /// - Parameter value: <#value description#> /// - Returns: <#description#> static func moneyFormatterNotKeepTwoDecimal(_ value : String?) -> String{ if value == nil{ return "0.00" } let result = Double(value!) if result?.isNaN ?? true{ return "0.00" } return String.init(format: "%f", (result ?? 0)) } static func moneyFormatterNotKeepTwoDecimal(_ value : Double?) -> String{ if value == nil{ return "0.00" } if value?.isNaN ?? true{ return "0.00" } return String.init(format: "%f", value!) } } func ++(str:String?, str1:String?) -> String{ guard str == nil && str1 == nil else { guard let _ = str else { return String.moneyFormatter(str1) } guard let _ = str1 else { return String.moneyFormatter(str) } let number = NSDecimalNumber.init(string: str) let number1 = NSDecimalNumber.init(string: str1) if number.decimalValue.isNaN{ return String.moneyFormatter(number1.stringValue) } if number1.decimalValue.isNaN{ return String.moneyFormatter(number.stringValue) } return String.moneyFormatter(number.adding(number1).stringValue) } return "0.00" } func ++!(str:String?, str1:String?) -> String{ guard str == nil && str1 == nil else { guard let _ = str else { return String.moneyFormatterNotKeepTwoDecimal(str1) } guard let _ = str1 else { return String.moneyFormatterNotKeepTwoDecimal(str) } let number = NSDecimalNumber.init(string: str) let number1 = NSDecimalNumber.init(string: str1) if number.decimalValue.isNaN{ return String.moneyFormatterNotKeepTwoDecimal(number1.stringValue) } if number1.decimalValue.isNaN{ return String.moneyFormatterNotKeepTwoDecimal(number.stringValue) } return String.moneyFormatterNotKeepTwoDecimal(number.adding(number1).stringValue) } return "0.00" } func --(str:String?, str1:String?) -> String{ guard str == nil && str1 == nil else { guard let _ = str else { return "-" + String.moneyFormatter(str1) } guard let _ = str1 else { return String.moneyFormatter(str) } let number = NSDecimalNumber.init(string: str) let number1 = NSDecimalNumber.init(string: str1) if number1.decimalValue.isNaN{ return String.moneyFormatter(number.stringValue) } if number.decimalValue.isNaN{ if number.decimalValue.isZero{ return "0.00" } return "-" + String.moneyFormatter(number1.stringValue) } return String.moneyFormatter(number.subtracting(number1).stringValue) } return "0.00" } func --!(str:String?, str1:String?) -> String{ guard str == nil && str1 == nil else { guard let _ = str else { return "-" + String.moneyFormatterNotKeepTwoDecimal(str1) } guard let _ = str1 else { return String.moneyFormatterNotKeepTwoDecimal(str) } let number = NSDecimalNumber.init(string: str) let number1 = NSDecimalNumber.init(string: str1) if number1.decimalValue.isNaN{ return String.moneyFormatterNotKeepTwoDecimal(number.stringValue) } if number.decimalValue.isNaN{ if number.decimalValue.isZero{ return "0.00" } return "-" + String.moneyFormatterNotKeepTwoDecimal(number1.stringValue) } return String.moneyFormatterNotKeepTwoDecimal(number.subtracting(number1).stringValue) } return "0.00" } func **(str:String?, str1:String?) -> String{ guard str == nil || str1 == nil else { let number = NSDecimalNumber.init(string: str) let number1 = NSDecimalNumber.init(string: str1) if number.decimalValue.isNaN || number1.decimalValue.isNaN{ return "0.00" } return String.moneyFormatter(number.multiplying(by:number1).stringValue) } return "0.00" } func **!(str:String?, str1:String?) -> String{ guard str == nil || str1 == nil else { let number = NSDecimalNumber.init(string: str) let number1 = NSDecimalNumber.init(string: str1) if number.decimalValue.isNaN || number1.decimalValue.isNaN{ return "0.00" } return String.moneyFormatterNotKeepTwoDecimal(number.multiplying(by:number1).stringValue) } return "0.00" } func /(str:String?, str1:String?) -> String{ if str1 == nil{ return "0.00" } let number1 = NSDecimalNumber.init(string: str1) let number = NSDecimalNumber.init(string: str) if number.decimalValue.isNaN || number1.decimalValue.isNaN{ return "0.00" } if number1.decimalValue.isZero{ return "0.00" } return String.moneyFormatter(number.dividing(by:number1).stringValue) } func /!(str:String?, str1:String?) -> String{ if str1 == nil{ return "0.00" } let number1 = NSDecimalNumber.init(string: str1) let number = NSDecimalNumber.init(string: str) if number.decimalValue.isNaN || number1.decimalValue.isNaN{ return "0.00" } if number1.decimalValue.isZero{ return "0.00" } return String.moneyFormatterNotKeepTwoDecimal(number.dividing(by:number1).stringValue) } func <(str:String?,number:Any) -> Bool{ let firstStr = NSDecimalNumber.init(string: str++"") if number is Int{ if firstStr.compare(NSNumber.init(value: number as! Int)) == ComparisonResult.orderedAscending{ return true } } else if number is Double{ if firstStr.compare(NSNumber.init(value: number as! Double)) == ComparisonResult.orderedAscending{ return true } } else if number is Float{ if firstStr.compare(NSNumber.init(value: number as! Float)) == ComparisonResult.orderedAscending{ return true } } return false } func <=(str:String?,number:Any)->Bool{ let firstStr = NSDecimalNumber.init(string: str++"") if number is Int{ if firstStr.compare(NSNumber.init(value: number as! Int)) != ComparisonResult.orderedDescending{ return true } } else if number is Double{ if firstStr.compare(NSNumber.init(value: number as! Double)) != ComparisonResult.orderedDescending{ return true } } else if number is Float{ if firstStr.compare(NSNumber.init(value: number as! Float)) != ComparisonResult.orderedDescending{ return true } } return false } func >(str:String?,number:Any) -> Bool{ let firstStr = NSDecimalNumber.init(string: str++"") if number is Int{ if firstStr.compare(NSNumber.init(value: number as! Int)) == ComparisonResult.orderedDescending{ return true } } else if number is Double{ if firstStr.compare(NSNumber.init(value: number as! Double)) == ComparisonResult.orderedDescending{ return true } } else if number is Float{ if firstStr.compare(NSNumber.init(value: number as! Float)) == ComparisonResult.orderedDescending{ return true } } return false } func >=(str:String?,number:Any)->Bool{ let firstStr = NSDecimalNumber.init(string: str++"") if number is Int{ if firstStr.compare(NSNumber.init(value: number as! Int)) != ComparisonResult.orderedAscending{ return true } } else if number is Double{ if firstStr.compare(NSNumber.init(value: number as! Double)) != ComparisonResult.orderedAscending{ return true } } else if number is Float{ if firstStr.compare(NSNumber.init(value: number as! Float)) != ComparisonResult.orderedAscending{ return true } } return false } func === (str:String?,number:Any)->Bool{ let firstStr = NSDecimalNumber.init(string: str++"") if number is Int{ if firstStr.compare(NSNumber.init(value: number as! Int)) == ComparisonResult.orderedSame{ return true } } else if number is Double{ if firstStr.compare(NSNumber.init(value: number as! Double)) == ComparisonResult.orderedSame{ return true } } else if number is Float{ if firstStr.compare(NSNumber.init(value: number as! Float)) == ComparisonResult.orderedSame{ return true } } return false } func != (str:String?,number:Any)->Bool{ let firstStr = NSDecimalNumber.init(string: str++"") if number is Int{ if firstStr.compare(NSNumber.init(value: number as! Int)) != ComparisonResult.orderedSame{ return true } } else if number is Double{ if firstStr.compare(NSNumber.init(value: number as! Double)) != ComparisonResult.orderedSame{ return true } } else if number is Float{ if firstStr.compare(NSNumber.init(value: number as! Float)) != ComparisonResult.orderedSame{ return true } } return false }