Skip to content

Commit

Permalink
fix displaying "-$0"
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Jan 24, 2024
1 parent 165b5a8 commit ed4f2e0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1520"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO"
parallelizable = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "02841E3229AD6E5500C0E7CC"
BuildableName = "dydxFormatterTests.xctest"
BlueprintName = "dydxFormatterTests"
ReferencedContainer = "container:dydxFormatter.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
27 changes: 15 additions & 12 deletions dydx/dydxFormatter/dydxFormatter/dydxFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ public final class dydxFormatter: NSObject, SingletonProtocol {
.store(in: &subscriptions)
}

private var priceFormatter: NumberFormatter = {
private func priceFormatter(withDigits digits: Int) -> NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
formatter.numberStyle = .currency
formatter.minimumFractionDigits = digits
formatter.maximumFractionDigits = digits
formatter.currencySymbol = "$"
return formatter
}()
}

private var significantDigitsFormatter: NumberFormatter = {
let formatter = NumberFormatter()
Expand Down Expand Up @@ -279,14 +280,16 @@ public final class dydxFormatter: NSObject, SingletonProtocol {
}

public func dollar(number: NSNumber?, digits: Int) -> String? {
if let dollar = localFormatted(number: number?.abs(), digits: digits) {
if (number?.doubleValue ?? Double.zero) >= Double.zero {
return "$\(dollar)"
} else {
return "-$\(dollar)"
}
let priceFormatter = priceFormatter(withDigits: digits)
guard let number = number,
let formatted = priceFormatter.string(from: number) else {
return nil
}
// need to special case for negative 0, see dydxFormatter tests. E.g. "-$0.001" should go to "$0.00"
if priceFormatter.number(from: formatted) == 0 {
return priceFormatter.string(from: 0)
} else {
return nil
return formatted
}
}

Expand Down
48 changes: 42 additions & 6 deletions dydx/dydxFormatter/dydxFormatterTests/dydxFormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,48 @@ final class dydxFormatterTests: XCTestCase {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
func testDollarFormatting() throws {
var number: Double = 1
var digits = 2
var formatted = dydxFormatter.shared.dollar(number: number, digits: digits)
var expected = "$1.00"
XCTAssertEqual(formatted, expected)

number = -0.001
digits = 0
formatted = dydxFormatter.shared.dollar(number: number, digits: digits)
expected = "$0"
XCTAssertEqual(formatted, expected)

number = -0.001
digits = 2
formatted = dydxFormatter.shared.dollar(number: number, digits: digits)
expected = "$0.00"
XCTAssertEqual(formatted, expected)

number = -0.001
digits = 3
formatted = dydxFormatter.shared.dollar(number: number, digits: digits)
expected = "-$0.001"
XCTAssertEqual(formatted, expected)

number = 0.001
digits = 2
formatted = dydxFormatter.shared.dollar(number: number, digits: digits)
expected = "$0.00"
XCTAssertEqual(formatted, expected)

number = -0.005
digits = 2
formatted = dydxFormatter.shared.dollar(number: number, digits: digits)
expected = "$0.00"
XCTAssertEqual(formatted, expected)

number = -0.0051
digits = 2
formatted = dydxFormatter.shared.dollar(number: number, digits: digits)
expected = "-$0.01"
XCTAssertEqual(formatted, expected)
}

func testPerformanceExample() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class dydxTransferAlertsProvider: dydxBaseAlertsProvider, dydxCustomAlertsProvid
}

private func createTransferStatusAlertItem(transfer: dydxTransferInstance) -> PlatformViewModel? {
let usdcSize = dydxFormatter.shared.dollar(number: transfer.usdcSize, digits: 2)
let usdcSize = dydxFormatter.shared.dollar(number: transfer.usdcSize)
let size = dydxFormatter.shared.raw(number: Parser.standard.asNumber(transfer.size), digits: 2)

let title: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private class dydxTransferStatusViewPresenter: HostedViewPresenter<dydxTransferS
let size = parser.asNumber(transfer.usdcSize ?? transferInput?.size?.usdcSize)?.doubleValue
if transferInput != nil {
viewModel?.title = DataLocalizer.localize(path: "APP.V4_DEPOSIT.IN_PROGRESS_TITLE")
let params = ["AMOUNT_ELEMENT": dydxFormatter.shared.dollar(number: size, digits: 2) ?? ""]
let params = ["AMOUNT_ELEMENT": dydxFormatter.shared.dollar(number: size) ?? ""]
viewModel?.text = DataLocalizer.localize(path: "APP.V4_DEPOSIT.IN_PROGRESS_TEXT", params: params)
} else {
viewModel?.title = DataLocalizer.localize(path: "APP.V4_DEPOSIT.CHECK_STATUS_TITLE")
Expand Down Expand Up @@ -173,7 +173,7 @@ private class dydxTransferStatusViewPresenter: HostedViewPresenter<dydxTransferS
deleteTransferInstance(transactionHash: transactionHash)
} else {
let size = parser.asNumber(transfer.usdcSize ?? transferInput?.size?.usdcSize)?.doubleValue
let params = ["AMOUNT_ELEMENT": dydxFormatter.shared.dollar(number: size, digits: 2) ?? ""]
let params = ["AMOUNT_ELEMENT": dydxFormatter.shared.dollar(number: size) ?? ""]
viewModel?.title = DataLocalizer.localize(path: "APP.V4_DEPOSIT.IN_PROGRESS_TITLE")
viewModel?.text = DataLocalizer.localize(path: "APP.V4_DEPOSIT.IN_PROGRESS_TEXT", params: params) + " " + status.statusText

Expand Down Expand Up @@ -204,7 +204,7 @@ private class dydxTransferStatusViewPresenter: HostedViewPresenter<dydxTransferS
let size = parser.asNumber(transfer.usdcSize ?? transferInput?.size?.usdcSize)?.doubleValue
if transferInput != nil {
viewModel?.title = DataLocalizer.localize(path: "APP.V4_WITHDRAWAL.IN_PROGRESS_TITLE")
let params = ["AMOUNT_ELEMENT": dydxFormatter.shared.dollar(number: size, digits: 2) ?? ""]
let params = ["AMOUNT_ELEMENT": dydxFormatter.shared.dollar(number: size) ?? ""]
viewModel?.text = DataLocalizer.localize(path: "APP.V4_WITHDRAWAL.IN_PROGRESS_TEXT", params: params)
} else {
viewModel?.title = DataLocalizer.localize(path: "APP.V4_WITHDRAWAL.CHECK_STATUS_TITLE")
Expand Down Expand Up @@ -234,7 +234,7 @@ private class dydxTransferStatusViewPresenter: HostedViewPresenter<dydxTransferS
deleteTransferInstance(transactionHash: transactionHash)
} else {
let size = parser.asNumber(transfer.usdcSize ?? transferInput?.size?.usdcSize)?.doubleValue
let params = ["AMOUNT_ELEMENT": dydxFormatter.shared.dollar(number: size, digits: 2) ?? ""]
let params = ["AMOUNT_ELEMENT": dydxFormatter.shared.dollar(number: size) ?? ""]
viewModel?.title = DataLocalizer.localize(path: "APP.V4_WITHDRAWAL.IN_PROGRESS_TITLE")
viewModel?.text = DataLocalizer.localize(path: "APP.V4_WITHDRAWAL.IN_PROGRESS_TEXT", params: params) + " " + status.statusText

Expand Down Expand Up @@ -268,7 +268,7 @@ private class dydxTransferStatusViewPresenter: HostedViewPresenter<dydxTransferS
let params: [String: String]
if let usdcSize = transfer.usdcSize {
params = [
"AMOUNT_ELEMENT": dydxFormatter.shared.dollar(number: usdcSize, digits: 2) ?? "",
"AMOUNT_ELEMENT": dydxFormatter.shared.dollar(number: usdcSize) ?? "",
"TOKEN": dydxTokenConstants.usdcTokenName
]
} else if let size = transfer.size {
Expand Down

0 comments on commit ed4f2e0

Please sign in to comment.