Skip to content

Commit

Permalink
add variable precision to leverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Jun 27, 2024
1 parent 8c7ac3a commit 9106407
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
29 changes: 18 additions & 11 deletions dydx/dydxFormatter/dydxFormatter/dydxFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,12 @@ public final class dydxFormatter: NSObject, SingletonProtocol {
}

/*
xxxxxx,yyyyy or xxxxx.yyyyy
will take the number and round it to the closest step size
e.g. if number is 1021 and step size is "100" then output is "1000"
converts number in multiplier, e.g.
1.20 -> 1.2x
removes trailing 0s as well
*/
public func multiplier(number: Double?) -> String? {
if let formattedText = dydxFormatter.shared.raw(number: number, digits: 2) {
public func multiplier(number: Double?, maxPrecision: Int = 2) -> String? {
if let formattedText = dydxFormatter.shared.raw(number: number, minDigits: 0, maxDigits: maxPrecision) {
return "\(formattedText)×"
} else {
return nil
Expand Down Expand Up @@ -421,19 +421,26 @@ public final class dydxFormatter: NSObject, SingletonProtocol {

public func raw(number: Double?, digits: Int, locale: Locale = Locale.current) -> String? {
guard let number = number else { return nil }
return raw(number: NSNumber(value: number), digits: digits, locale: locale)
return raw(number: NSNumber(value: number), minDigits: digits, maxDigits: digits, locale: locale)
}

public func raw(number: Double?, minDigits: Int, maxDigits: Int, locale: Locale = Locale.current) -> String? {
guard let number = number else { return nil }
return raw(number: NSNumber(value: number), minDigits: minDigits, maxDigits: maxDigits, locale: locale)
}

/*
xxxxxx,yyyyy or xxxxx.yyyyy
*/
public func raw(number: NSNumber?, digits: Int, locale: Locale = Locale.current) -> String? {
guard let number = number else { return nil }
return raw(number: number, minDigits: digits, maxDigits: digits, locale: locale)
}

public func raw(number: NSNumber?, minDigits: Int, maxDigits: Int, locale: Locale = Locale.current) -> String? {
if let value = number?.doubleValue {
if value.isFinite {
if let number = number {
rawFormatter.locale = locale
rawFormatter.minimumFractionDigits = max(digits, 0)
rawFormatter.maximumFractionDigits = max(digits, 0)
rawFormatter.minimumFractionDigits = max(minDigits, 0)
rawFormatter.maximumFractionDigits = max(maxDigits, 0)
rawFormatter.roundingMode = .halfUp

let formatted = rawFormatter.string(from: number)
Expand Down
26 changes: 26 additions & 0 deletions dydx/dydxFormatter/dydxFormatterTests/dydxFormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,32 @@ final class dydxFormatterTests: XCTestCase {
}
}

func testMultiplierFormatting() throws {
struct TestCase {
let number: Double
let maxPrecision: Int
let expected: String
}

let testCases: [TestCase] = [
.init(number: 0.01, maxPrecision: 2, expected: "0.01×"),
.init(number: -0.01, maxPrecision: 2, expected: "-0.01×"),
.init(number: 0.01, maxPrecision: 3, expected: "0.01×"),
.init(number: -0.01, maxPrecision: 3, expected: "-0.01×"),
.init(number: 0.01, maxPrecision: 1, expected: ""),
.init(number: -0.01, maxPrecision: 1, expected: ""),
.init(number: 10, maxPrecision: 3, expected: "10×"),
.init(number: -10, maxPrecision: 3, expected: "-10×")
]

for testCase in testCases {
let formatted = dydxFormatter.shared.multiplier(number: testCase.number, maxPrecision: testCase.maxPrecision)
print(testCase)
XCTAssertEqual(formatted, testCase.expected)
print()
}
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ class dydxTradeInputMarginViewPresenter: HostedViewPresenter<dydxTradeInputMargi
private func update(withTradeInput tradeInput: Abacus.TradeInput) {
viewModel?.shouldDisplayTargetLeverage = tradeInput.options?.needsTargetLeverage == true
viewModel?.marginMode = DataLocalizer.localize(path: "APP.GENERAL.\(tradeInput.marginMode.rawValue)")
viewModel?.targetLeverage = dydxFormatter.shared.raw(number: tradeInput.targetLeverage, digits: 2)
viewModel?.targetLeverage = dydxFormatter.shared.raw(number: tradeInput.targetLeverage, minDigits: 0, maxDigits: 2)
}
}

0 comments on commit 9106407

Please sign in to comment.