//
//  DataTypeConversion.swift
//  WOKA
//
//  Created by MacBook Pro on 30/05/24.
//

import UIKit

// MARK: - Rounding Double

extension Double {
    var threeDigits: Double {
        return (self * 1000).rounded(.toNearestOrEven) / 1000
    }
    
    var twoDigits: Double {
        return (self * 100).rounded(.toNearestOrEven) / 100
    }
    
    var oneDigit: Double {
        return (self * 10).rounded(.toNearestOrEven) / 10
    }
}

//MARK: - Int To String
extension Int{
    func toString() -> String{
        let myString = String(self)
        return myString
    }
    
    func strFormat(decimal : Int)-> String{
        return String(format: "%.\(decimal)f%", self)
    }
}

extension Float{
    func strFormat(decimal : Int)-> String{
        return String(format: "%.\(decimal)f%", self)
    }
}

//MARK: - String To Double

extension String {
    func toDouble() -> Double? {
        return NumberFormatter().number(from: self)?.doubleValue
    }
}

//MARK: - Double To String
extension Double {
    func toString() -> String {
        return String(format: "%.2f",self)
    }
    
    func simpleString() -> String {
        return String(self)
    }
    
    func strFormat(decimal : Int)-> String{
        return String(format: "%.\(decimal)f%", self)
    }
            
    func rounded(decimalPoint: Int) -> Double {
        let power = pow(10, Double(decimalPoint))
        return (self * power).rounded() / power
    }
}

//MARK: - Double To Int
extension Double {
    func toInt() -> Int {
        return Int(self)
    }
}

//MARK: - String To Int
extension String {
    func toInt() -> Int {
        return Int(Double(self) ?? 0.0)
    }
}

//MARK: - String To Int
extension String {
    func toCgFloat() -> CGFloat {
        let val = Float(self) ?? 0.0
        return CGFloat(val)
    }
}

//MARK: - Int To String
extension Int {
    func toCgFloat() -> CGFloat {
        return CGFloat(self)
    }
}

//MARK: - Int To Double
extension Int {
    func toDouble() -> Double {
        return Double(self)
    }
}

//MARK: - filter number from string

extension String {
    var numbers: String {
        return filter { "0"..."9" ~= $0 }
    }
}

extension Double {
    func round(to decimalPlaces: Int) -> Double {
        let precisionNumber = pow(10,Double(decimalPlaces))
        var n = self // self is a current value of the Double that you will round
        n = n * precisionNumber
        n.round()
        n = n / precisionNumber
        return n
    }
}
