-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
228 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import Foundation | ||
|
||
public struct Hbar { | ||
private let tinybar: Int64 | ||
|
||
private init(tinybar: Int64) { | ||
self.tinybar = tinybar | ||
} | ||
|
||
public static let MAX = Hbar(tinybar: Int64.max) | ||
public static let MIN = Hbar(tinybar: Int64.min) | ||
public static let ZERO = Hbar(tinybar: 0) | ||
|
||
public init?(hbar: Decimal) { | ||
let tinybarAmount = hbar * HbarUnit.Hbar.toTinybarCount() | ||
guard let tinybar = Int64(exactly: NSDecimalNumber(decimal: tinybarAmount)) else { return nil } | ||
|
||
self.init(tinybar: tinybar) | ||
} | ||
|
||
public static func from(amount: Decimal, unit: HbarUnit) -> Self? { | ||
guard let tinybar = Int64(exactly: NSDecimalNumber(decimal: amount * unit.toTinybarCount())) else { return nil } | ||
|
||
return Self(tinybar: tinybar) | ||
} | ||
|
||
public static func from(amount: Int64, unit: HbarUnit) -> Self? { | ||
guard let tinybar = Int64(exactly: NSDecimalNumber(decimal: Decimal(amount) * unit.toTinybarCount())) else { return nil } | ||
|
||
return Self(tinybar: tinybar) | ||
} | ||
|
||
public static func fromTinybar(amount: Int64) -> Self { | ||
Self(tinybar: amount) | ||
} | ||
|
||
public func `as`(unit: HbarUnit) -> Decimal { | ||
if unit == .Tinybar { | ||
return Decimal(tinybar) | ||
} | ||
return Decimal(tinybar) / unit.toTinybarCount() | ||
} | ||
|
||
public func asTinybar() -> Int64 { | ||
return tinybar | ||
} | ||
} | ||
|
||
extension Hbar: CustomStringConvertible { | ||
public var description: String { | ||
if Decimal(tinybar) < HbarUnit.Hbar.toTinybarCount() { | ||
return "\(tinybar) \(HbarUnit.Tinybar.getSymbol)" | ||
} | ||
return "\(Decimal(tinybar) / HbarUnit.Hbar.toTinybarCount()) \(HbarUnit.Hbar.getSymbol) (\(tinybar) tinybar)" | ||
} | ||
} | ||
|
||
extension Hbar: Comparable { | ||
public static func < (lhs: Hbar, rhs: Hbar) -> Bool { | ||
return lhs.tinybar < rhs.tinybar | ||
} | ||
|
||
public static func == (lhs: Hbar, rhs: Hbar) -> Bool { | ||
return lhs.tinybar == rhs.tinybar | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Foundation | ||
|
||
public enum HbarUnit { | ||
case Tinybar | ||
case Microbar | ||
case Millibar | ||
case Hbar | ||
case Kilobar | ||
case Megabar | ||
case Gigabar | ||
|
||
public var getSymbol: String { | ||
switch self { | ||
case .Tinybar: | ||
return "tℏ" | ||
case .Microbar: | ||
return "μℏ" | ||
case .Millibar: | ||
return "mℏ" | ||
case .Hbar: | ||
return "ℏ" | ||
case .Kilobar: | ||
return "kℏ" | ||
case .Megabar: | ||
return "Mℏ" | ||
case .Gigabar: | ||
return "Gℏ" | ||
} | ||
} | ||
|
||
func toTinybarCount() -> Decimal { | ||
let amount: UInt64 | ||
switch self { | ||
case .Tinybar: | ||
amount = 1 | ||
case .Microbar: | ||
amount = 100 | ||
case .Millibar: | ||
amount = 100_000 | ||
case .Hbar: | ||
amount = 100_000_000 | ||
case .Kilobar: | ||
amount = 100_000_000 * 1_000 | ||
case .Megabar: | ||
amount = 100_000_000 * 1_000_000 | ||
case .Gigabar: | ||
amount = 100_000_000 * 1_000_000_000 | ||
} | ||
|
||
return Decimal(amount) | ||
} | ||
} | ||
|
||
extension HbarUnit: CustomStringConvertible { | ||
public var description: String { | ||
switch self { | ||
case .Tinybar: | ||
return "tinybar" | ||
case .Microbar: | ||
return "microbar" | ||
case .Millibar: | ||
return "millibar" | ||
case .Hbar: | ||
return "hbar" | ||
case .Kilobar: | ||
return "kilobar" | ||
case .Megabar: | ||
return "megabar" | ||
case .Gigabar: | ||
return "gigabar" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.