An amazing project that generates micro reports from tournament results
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.0 KiB

//
// Formatters.swift
// TournamentStats
//
// Created by Laurent Morvillier on 03/06/2019.
// Copyright © 2019 Stax River. All rights reserved.
//
import Foundation
class Formatter {
static var currency: NumberFormatter {
let c = NumberFormatter()
c.locale = Locale(identifier: "en_US")
c.usesGroupingSeparator = true
c.groupingSeparator = ","
c.decimalSeparator = "."
c.currencySymbol = "$"
c.maximumFractionDigits = 0
c.numberStyle = .currency
return c
}
static var importData: NumberFormatter {
let formatter = NumberFormatter()
formatter.usesGroupingSeparator = true
formatter.groupingSeparator = ","
formatter.decimalSeparator = "."
formatter.numberStyle = .decimal
return formatter
}
static var basicDate: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
return formatter
}
static var fileDate: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy_MM_dd"
return formatter
}
}
extension Int {
var currencyFormatted: String {
return Formatter.currency.string(from: NSNumber(value: self)) ?? ""
}
var rankFormatted: String {
switch self {
case 11:
return "11th"
case 12:
return "12th"
case 13:
return "13th"
default:
let unit = "\(self)"
let n = Int(String(unit.last!))
switch n {
case 1:
return "\(self)st"
case 2:
return "\(self)nd"
case 3:
return "\(self)rd"
default:
return "\(self)th"
}
}
}
}
extension Double {
var currencyFormatted: String {
return Formatter.currency.string(from: NSNumber(value: self)) ?? ""
}
}