XFormatter

1.0.1

XFormatter creates localized strings from numbers, currencies, dates and more.
pawello2222/XFormatter

What's New

1.0.1

2023-07-24T21:03:28Z

XFormatter logo

Build Code coverage Language Supported platforms

Swift Package Manager Release version License

XFormatter

XFormatter (eXtended Formatter) creates localized strings from numbers, currencies, dates and more.

Table of Contents
  1. Installation
  2. Highlights
  3. License

Installation

Requirements

  • iOS 16.0+
  • macOS 13.0+
  • watchOS 9.0+
  • tvOS 16.0+

Swift Package Manager

XFormatter is available as a Swift Package.

.package(url: "https://github.com/pawello2222/XFormatter.git", .upToNextMajor(from: "1.0.0"))

Highlights

Currency

let formatter = XFormatter.currency(
    locale: .init(identifier: "en_US"),
    currencyCode: "USD"
)

XCTAssertEqual(formatter.string(from: 326.097, abbreviation: .default), "$326.10")
XCTAssertEqual(formatter.string(from: 1432.99, abbreviation: .default), "$1.43k")
XCTAssertEqual(formatter.string(from: 100_081, abbreviation: .default), "$100.08k")
XCTAssertEqual(formatter.string(from: 4_729_432, abbreviation: .default), "$4.73m")
XCTAssertEqual(formatter.string(from: -42.811, abbreviation: .default), "-$42.81")
XCTAssertEqual(formatter.string(from: -4239.81, abbreviation: .default), "-$4.24k")
XCTAssertEqual(formatter.string(from: 123.456, sign: .arrow), "▲$123.46")

Decimal

let formatter = XFormatter.decimal(
    locale: .init(identifier: "en_US")
)

XCTAssertEqual(formatter.string(from: -1000), "-1,000")
XCTAssertEqual(formatter.string(from: 1000, abbreviation: .default), "1k")
XCTAssertEqual(formatter.string(from: 1000, abbreviation: .capitalized), "1K")
XCTAssertEqual(formatter.string(from: 123.456, sign: .both), "+123.46")
XCTAssertEqual(formatter.string(from: -123.456, sign: .spacedArrow), "▼ 123.46")
XCTAssertEqual(formatter.string(from: 0.123456789, precision: .default), "0.12")
XCTAssertEqual(formatter.string(from: 0.12, precision: .init(3...)), "0.120")

Date

let formatter = XDateFormatter.date(
    locale: .init(identifier: "pl_PL"),
    localizedFormat: "yyyyMMddjjmmss"
)

let date = Date(year: 2000, month: 3, day: 24, hour: 16, minute: 14, second: 44)

XCTAssertEqual(formatter.string(from: date), "24.03.2000, 16:14:44")

Date components

let formatter = XDateFormatter.dateComponents(
    locale: .init(identifier: "en_US")
)

let now = Date()
let future = now.adjusting(\.day, by: 2)

XCTAssertEqual(formatter.string(from: now, to: future), "48 hours")
let formatter = XDateFormatter.dateComponents(
    locale: .init(identifier: "en_US")
)

let date = Date(year: 2000, month: 3, day: 24, hour: 16, minute: 14, second: 44)
let components = date.components([.hour, .minute])

XCTAssertEqual(formatter.string(from: components), "16 hours, 14 minutes")

License

XFormatter is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Fri Apr 12 2024 17:42:41 GMT-0900 (Hawaii-Aleutian Daylight Time)