//
//  Double.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 08/03/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//

import Foundation
import CoreGraphics

extension Double {
    
    func format(f: String) -> String {
        return String(format: "%\(f)", self)
    }

    func format(decimalDigits: Int) -> String {
        return String(format: "%.\(decimalDigits)f", self)
    }

    func format(digits: Int) -> String {
        for d in 0...digits {
            if self < pow(10, d).doubleValue {
                return self.format(f: ".\(digits - d)f")
            }
        }
        return self.format(f: ".0f")
    }
    
    var uInt8Value: UInt8 {
        return UInt8(self)
    }

    var intValue: Int {
        return Int(self)
    }

    var CGFloatValue: CGFloat {
        return CGFloat(self)
    }

    func format(fractionDigits: Int) ->String {
        let nf = NumberFormatter()
        nf.minimumFractionDigits = fractionDigits
        nf.maximumFractionDigits = fractionDigits
        return nf.string(from: NSNumber(value: self))!
    }
}
