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