All Projects → Flight-School → Guide-to-Swift-Numbers-Sample-Code

Flight-School / Guide-to-Swift-Numbers-Sample-Code

Licence: MIT license
Xcode Playground Sample Code for the Flight School Guide to Swift Numbers

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Guide-to-Swift-Numbers-Sample-Code

Currency.js
A javascript library for handling currencies
Stars: ✭ 2,214 (+2306.52%)
Mutual labels:  money, currency
Prices
Python price handling for humans.
Stars: ✭ 248 (+169.57%)
Mutual labels:  money, currency
Jackson Datatype Money
Extension module to properly support datatypes of javax.money
Stars: ✭ 165 (+79.35%)
Mutual labels:  money, currency
Easymoney
Library for operating with monetary values in JavaScript and Typescript 💵
Stars: ✭ 145 (+57.61%)
Mutual labels:  money, currency
stockholm
💵 Modern Python library for working with money and monetary amounts. Human friendly and flexible approach for development. 100% test coverage + built-in support for GraphQL and Protocol Buffers transports using current best-practices.
Stars: ✭ 26 (-71.74%)
Mutual labels:  money, currency
Ngx Currency
📦 Currency mask module for Angular
Stars: ✭ 161 (+75%)
Mutual labels:  money, currency
Jsr354 Ri
JSR 354 - Moneta: Reference Implementation
Stars: ✭ 223 (+142.39%)
Mutual labels:  money, currency
Javamoney Lib
JavaMoney financial libraries, extending and complementing JSR 354
Stars: ✭ 104 (+13.04%)
Mutual labels:  money, currency
nova-money-field
Money Field for Laravel Nova
Stars: ✭ 71 (-22.83%)
Mutual labels:  money, currency
CurrencyEditText
A simple EditText input designed to input decimal and currency values.
Stars: ✭ 18 (-80.43%)
Mutual labels:  money, currency
Django Prices
Django fields for the prices module
Stars: ✭ 135 (+46.74%)
Mutual labels:  money, currency
binarium
Binarium cryptocurrency is the first one protected from ASICs
Stars: ✭ 14 (-84.78%)
Mutual labels:  money, currency
Exchanger
🏢 Currency exchange rates framework for PHP
Stars: ✭ 133 (+44.57%)
Mutual labels:  money, currency
pesa
A JS money lib whose precision goes up to 11 (and beyond).
Stars: ✭ 38 (-58.7%)
Mutual labels:  money, currency
Frankfurter
💱 Currency data API
Stars: ✭ 123 (+33.7%)
Mutual labels:  money, currency
Cash Cli
💰💰 Convert currency rates directly from your terminal!
Stars: ✭ 168 (+82.61%)
Mutual labels:  money, currency
Countries
Countries - ISO 3166 (ISO3166-1, ISO3166, Digit, Alpha-2 and Alpha-3) countries codes and names (on eng and rus), ISO 4217 currency designators, ITU-T E.164 IDD calling phone codes, countries capitals, UN M.49 regions codes, ccTLD countries domains, IOC/NOC and FIFA letters codes, VERY FAST, NO maps[], NO slices[], NO init() funcs, NO external links/files/data, NO interface{}, NO specific dependencies, Databases/JSON/GOB/XML/CSV compatible, Emoji countries flags and currencies support, full support ISO-3166-1, ISO-4217, ITU-T E.164, Unicode CLDR and ccTLD standarts.
Stars: ✭ 85 (-7.61%)
Mutual labels:  money, currency
Money Open Exchange Rates
A gem that calculates the exchange rate using published rates from open-exchange-rates. Compatible with the money gem.
Stars: ✭ 87 (-5.43%)
Mutual labels:  money, currency
Megacoin
Welcome to Megacoin MΣC - Around the World!
Stars: ✭ 16 (-82.61%)
Mutual labels:  money, currency
bankster
Money Creation Made Easy
Stars: ✭ 30 (-67.39%)
Mutual labels:  money, currency
Flight School Guide to Swift Codable Cover

Guide to Swift Numbers Sample Code

Build Status License Swift Version

This repository contains sample code used in the Flight School Guide to Swift Numbers.


Chapter 1

Chapter 1 is a conceptual deep-dive into how numbers work on computers in general and in Swift specifically.

Floating Point Approximates

You know how floating-point arithmetic sometimes produces unexpected results, like 0.1 + 0.2 != 0.3? (If not, go ahead and read the first chapter for free!)

This sample code implements an "approximately equals" operator (==~) for floating-point number types.

0.1 + 0.2 == 0.3 // false
0.1 + 0.2 ==~ 0.3 // true

(0.1 + 0.2).isApproximatelyEqual(to: 0.3, within: .ulpOfOne) // true

Floating Point Environment

Normally, you can't tell the difference between nan and signalingNaN. That's because Swift doesn't expose the floating-point environment in its standard library.

We can still access it from Darwin, though. And that's what this playground demonstrates:

do {
    try detectingFloatingPointErrors(flags: .invalid) {
        Double.signalingNaN + 1
    }
} catch {
    print("Error: \(error)")
}

Chapter 2

Chapter 2 is all about number formatting. The sample code in this chapter offers a comprehensive survey of the various formatting styles of NumberFormatter, and how they work in different locales.

Cardinal Numbers

let formatter = NumberFormatter()
formatter.string(for: 4) // 4

formatter.numberStyle = .spellOut
formatter.string(for: 4) // four

Ordinal Numbers

let formatter = NumberFormatter()
formatter.numberStyle = .ordinal
formatter.string(for: 1) // 1st

Decimal Numbers

let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.string(for: 1234567.89) 1,234,567.89

Significant Digits

let formatter = NumberFormatter()
formatter.usesSignificantDigits = true

formatter.maximumSignificantDigits = 2

formatter.string(from: 123) // 120
formatter.string(from: 123456) // 120000
formatter.string(from: 123.456) // 120
formatter.string(from: 1.230000) // 1.2
formatter.string(from: 0.00123) // 0.0012

Integer and Fraction Digits

let formatter = NumberFormatter()
formatter.usesSignificantDigits = false // default

formatter.minimumIntegerDigits = 4
formatter.minimumFractionDigits = 2

formatter.string(from: 123) // 0123.00
formatter.string(from: 123456) // 123456.00
formatter.string(from: 123.456) // 0123.46
formatter.string(from: 1.230000) // 0001.23
formatter.string(from: 0.00123) // 0000.00

Rounding Modes

let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 1

let numbers = [1.2, 1.22, 1.25, 1.27, -1.25]
let modes: [NumberFormatter.RoundingMode] = [.ceiling, .floor, .up, .down, .halfUp, .halfDown, .halfEven]

for mode in modes {
    formatter.roundingMode = mode

    for number in numbers {
        formatter.string(for: number)
    }
}

Scientific Notation

let formatter = NumberFormatter()
formatter.numberStyle = .scientific
formatter.string(for: 12345.6789) // 1.23456789E4

Percentages

let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter.string(for: 0.12) // 12%

Currencies

let formatter = NumberFormatter()

let identifiers =
    ["en-US", "en-GB", "de-DE", "ja-JP"]
let styles: [NumberFormatter.Style] =
    [.currency, .currencyAccounting, .currencyISOCode, .currencyPlural]

for style in styles {
    formatter.numberStyle = style
    for identifier in identifiers {
        formatter.locale = Locale(identifier: identifier)
        formatter.string(for: 1234.567)
    }
}

Custom Formats

let formatter = NumberFormatter()
formatter.numberStyle = .decimal

// Format with thousands and decimal separator
// that rounds to the nearest five tenths
formatter.format = "#,##0.5"

formatter.locale = Locale(identifier: "en-US")
formatter.string(for: 1234.567) // 1,234.5

formatter.locale = Locale(identifier: "fr-FR")
formatter.string(for: 1234.567) // 1 234,5

Chapter 3

Chapter 3 shows the correct way to represent and work with money in code.

Money

let prices: [Money<USD>] = [2.19, 5.39, 20.99, 2.99, 1.99, 1.99, 0.99]
let subtotal = prices.reduce(0.00, +)
let tax = 0.08 * subtotal
let total = subtotal + tax // $39.45

Currency Converter

let EURtoUSD = CurrencyPair<EUR, USD>(rate: 1.17) // as of June 1st, 2018

let euroAmount: Money<EUR> = 123.45
let dollarAmount = EURtoUSD.convert(euroAmount) // $144.44

Chapter 4

Chapter 4 covers Foundation's units and measurements APIs.

Natural Scale

let lengthOfRoom = Measurement<UnitLength>(value: 8, unit: .meters)
let distanceToAirport = Measurement<UnitLength>(value: 16, unit: .kilometers)

let formatter = MeasurementFormatter()
formatter.unitOptions = .naturalScale
formatter.string(from: lengthOfRoom) // 26.247 ft
formatter.string(from: distanceToAirport) // 9.942 mi

Provided Units

let ingotMass = Measurement<UnitMass>(value: 400, unit: .ouncesTroy)

let formatter = MeasurementFormatter()
formatter.unitOptions = .providedUnit
formatter.string(from: ingotMass) // 400 oz t

Configuring Precision

let barometerReading = Measurement<UnitPressure>(value: 29.9, unit: .inchesOfMercury)
let pressureInMillibars = barometerReading.converted(to: .millibars)

let formatter = MeasurementFormatter()
formatter.unitOptions = .providedUnit
formatter.numberFormatter.usesSignificantDigits = true
formatter.numberFormatter.maximumSignificantDigits = 3
formatter.string(from: pressureInMillibars) // 1,010 mbar

Temperature Formatting

let temperatureInF = Measurement<UnitTemperature>(value: 72, unit: .fahrenheit)
let temperatureInC = Measurement<UnitTemperature>(value: 20.5, unit: .celsius)

let formatter = MeasurementFormatter()
formatter.locale = Locale(identifier: "en-US")
formatter.string(from: temperatureInF) // 72°F
formatter.string(from: temperatureInC) // 68.9°F

formatter.locale = Locale(identifier: "fr-FR")
formatter.string(from: temperatureInF) // 22,222 °C
formatter.string(from: temperatureInC) // 20,5 °C

formatter.unitOptions = .temperatureWithoutUnit

formatter.locale = Locale(identifier: "en-US")
formatter.string(from: temperatureInF) // 72°
formatter.string(from: temperatureInC) // 20.5° (!)

formatter.locale = Locale(identifier: "fr-FR")
formatter.string(from: temperatureInF) // 72° (!)
formatter.string(from: temperatureInC) // 20,5°

Units Interoperability

let loggedFlyingTime =
    Measurement<UnitDuration>(value: 220, unit: .hours)

let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day]
formatter.unitsStyle = .full
formatter.includesApproximationPhrase = true
formatter.string(from: loggedFlyingTime) // About 9 days

Chapter 5

Chapter 5 extends what we learned about units in the previous chapter to transform Xcode Playgrounds into an interactive physical calculator.

Agricultural Flight Planner

let takeoffWeight = (
    emptyPlaneWeight +
    payloadWeight +
    fuelWeight +
    pilotWeight
).converted(to: .pounds)

let canTakeOff = takeoffWeight < maximumTakeoffWeight // ???

License

MIT

About Flight School

Flight School is a book series for advanced Swift developers that explores essential topics in iOS and macOS development through concise, focused guides.

If you'd like to get in touch, feel free to message us on Twitter or email us at [email protected].

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].