123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- //
- // Convert.swift
- // HCQuanfangtong
- //
- // Created by Apple on 2021/12/24.
- // Copyright © 2021 Jyp. All rights reserved.
- //
- import Foundation
- /// 数据转换
- /// 任意类型转 Int,Double, Float, CGFloat, String
- class Convert{
- static func toInt(_ object: Any?) -> Int?{
- if object is Int{
- if let middle = object as? Int{
- return middle
- }
- }
- else if object is Double{
- if let middle = object as? Double{
- return Int.init(middle)
- }
- }
- else if object is Float{
- if let middle = object as? Float{
- return Int.init(middle)
- }
- }
- else if object is String{
- if let middle = object as? String{
- return Int.init(middle)
- }
- }
- return nil
- }
-
- static func toDouble(_ object: Any?) -> Double?{
- if object is Double{
- if let middle = object as? Double{
- return middle
- }
- }
- else if object is Float{
- if let middle = object as? Float{
- return Double.init(middle)
- }
- }
- else if object is String{
- if let middle = object as? String{
- return Double.init(middle)
- }
- }
- else if object is Int{
- if let middle = object as? Int{
- return Double.init(middle)
- }
- }
- return nil
- }
-
- static func toFloat(_ object: Any?) -> Float?{
-
- if object is Float{
- if let middle = object as? Float{
- return middle
- }
- }
- else if object is Double{
- if let middle = object as? Double{
- return Float.init(middle)
- }
- }
- else if object is Int{
- if let middle = object as? Int{
- return Float.init(middle)
- }
- }
- else if object is String{
- if let middle = object as? String{
- return Float.init(middle)
- }
- }
-
-
- return nil
- }
-
- static func toString(_ object: Any?, decimalNumber: Int? = nil) -> String?{
-
- if object is Int{
- if let middle = object as? Int{
- if decimalNumber.isNil{
- return String.init(format: "%ld", middle)
- }
- return String.init(format: String.init(format:"%%.%df", decimalNumber!), Convert.toFloat(middle) ?? 0)
- }
- }
- else if object is Float{
- if let middle = object as? Float{
- if decimalNumber.isNil{
- return String.init(format: "%f", middle)
- }
- return String.init(format: String.init(format:"%%.%df", decimalNumber!), middle)
- }
- }
- else if object is Double{
- if let middle = object as? Double{
- if decimalNumber.isNil{
- return String.init(format: "%f", middle)
- }
- return String.init(format: String.init(format:"%%.%df", decimalNumber!), middle)
- }
- }
-
- else if object is String{
-
- if decimalNumber.isNotNil{
- if let middle = Convert.toDouble(object){
- return String.init(format: String.init(format:"%%.%df", decimalNumber!), middle)
- }
- }
- else{
- return object as? String
- }
-
- }
- return nil
- }
- static func toCGFloat(_ object: Any?) -> CGFloat{
- return CGFloat.init(Convert.toFloat(object) ?? 0)
- }
-
- }
|