Meaningful usage of this framework requires understanding what Roman numerals are. Background information can be found on Wikipedia.
Features
Constants provided for all 3,999 standard Roman numerals.
Support for subtractive and additive notations.
Arithmetic using Roman-numeral-oriented algorithms - no integer calculations!
Conversions to-and-from popular types (e.g. String, Int).
Extensions for real-world usage (e.g. copyright text).
Conformance to all applicable numeric protocols.
Limitations
Fixed Numerical Range
Standard Roman numerals as we understand them were limited to values from 1 to 3,999. There is no concept of 0. Modern scholars have proposed extensions of the numeric system to support values greater than 3,999 but we do not recognize any of these extensions and decry the proposers to be heretics.
Most programs do not deal with numbers higher than 3,999, and the world won’t exist past the year 3,999 in the Gregorian calendar, so there is no need to worry.
iPhone Model Names
RomanNumeralKit does not support conversions to-and-from recent iPhone model names such as “Xs”.
Import RomanNumeralKit at the top of the Swift file you want to use it in.
import RomanNumeralKit
Constants
Constants are provided for all valid Roman numerals, from 1 to 3,999. You should never need to use an initializer
unless you are doing conversions from other types.
All constants can be accessed directly by their using their uppercase Unicode characters.
print(MMCDIX) // Prints "MMCDIX"
print(MMCDIX.symbols) // Prints "[M, M, C, D, I, X]"
XCTAssertEqual(MMCDIX, RomanNumeral(.M, .M, .C, .D, .I, .X)) // True
Conversions
We provide convenient mechanisms to convert RomanNumerals to-and-from popular types.
It should be noted that these are true conversions: the backing values of RomanNumeral instances are groups of tally
marks. We do not hold Intreferences because it would not be in the spirit of the framework.
Constructors
Constructors are provided to convert Ints and Strings to RomanNumerals.
print(RomanNumeral(from: 2409)) // Prints "MMCDIX"
print(RomanNumeral(from: "MMCDIX")) // Prints "MMCDIX"
We also support conversions from Int and String literals when the RomanNumeral type can be inferred.
let numeralFromInt: RomanNumeral = 2409
let numeralFromString: RomanNumeral = "MMCDIX"
print(numeralFromInt) // Prints "MMCDIX"
print(numeralFromString) // Prints "MMCDIX"
Properties
Instance-level properties are provided to convert RomanNumerals into Int and String values.
print(MMCDIX.intValue) // Prints "2409"
print(MMCDIX.stringValue) // Prints "MMCDIX"
We also provide various *Convertible protocols to allow types to return different RomanNumeral and
RomanNumeralSymbol representations of themselves.
Arithmetic
Addition, subtraction, and multiplication operations are supported (and required) thanks to our conformance to the
Numeric protocol. We use algorithms that allow us to directly manipulate the Roman numeral symbols as opposed to
doing conversions to-and-from Ints.
The default notation for this framework is subtractive notation - that is what instances of RomanNumerals represent.
We provide the AdditiveRomanNumeral struct for initialization of numerals using additive notation. We also support
conversions between the notations.
Both notations implement the RomanNumeralProtocol protocol and support the same general interface.
let additiveNumeral = AdditiveNotation(.M, .M, .C, .C, .C, .C, .V, .I, .I, .I, .I)
print(additiveNumeral) // Prints "MMCCCCVIIII"
print(additiveNumeral.intValue) // Prints "2409"
XCTAssertEqual(additiveRomanNumeral.romanNumeral, MMCDIX) // True
XCTAssertEqual(MMCDIX.additiveRomanNumeral, additiveRomanNumeral) // True
Extensions
We provide a variety of extensions on existing Swift types to make common operations easier.
Calendar & DateComponent Extensions
Calendar objects, and the DateComponents they produce, are able to convert years into RomanNumerals.
We conform Int and String to the *RomanNumeralConvertible protocols to complete the ouroboros with these
foundational types.
print(2409.romanNumeral) // Prints "MMCDIX"
print(2409.additiveRomanNumeral) // Prints "MMCCCCVIIII"
print("MMCDIX".romanNumeral) // Prints "MMCDIX"
print("MMCCCCVIIII".additiveRomanNumeral) // Prints "MMCCCCVIIII"
Authors
Kyle Hughes
Contributions
RomanNumeralKit is not accepting source contributions at this time.
License
RomanNumeralKit is available under the MIT License.
RomanNumeralKit
First-class Roman numeral support for Swift.
When in Rome, code as the Romans code.
Introduction
Meaningful usage of this framework requires understanding what Roman numerals are. Background information can be found on Wikipedia.
Features
String,Int).Limitations
Fixed Numerical Range
Standard Roman numerals as we understand them were limited to values from 1 to 3,999. There is no concept of 0. Modern scholars have proposed extensions of the numeric system to support values greater than 3,999 but we do not recognize any of these extensions and decry the proposers to be heretics.
Most programs do not deal with numbers higher than 3,999, and the world won’t exist past the year 3,999 in the Gregorian calendar, so there is no need to worry.
iPhone Model Names
RomanNumeralKit does not support conversions to-and-from recent iPhone model names such as “Xs”.
Requirements
Installation
CocoaPods
Add RomanNumeralKit to your
Podfile:Please visit the CocoaPods website for general CocoaPods usage and installation instructions.
Swift Package Manager
Add RomanNumeralKit to the
dependenciesvalue of yourPackage.swift:Usage
Import
RomanNumeralKitat the top of the Swift file you want to use it in.Constants
Constants are provided for all valid Roman numerals, from 1 to 3,999. You should never need to use an initializer unless you are doing conversions from other types.
All constants can be accessed directly by their using their uppercase Unicode characters.
Conversions
We provide convenient mechanisms to convert
RomanNumerals to-and-from popular types.It should be noted that these are true conversions: the backing values of
RomanNumeralinstances are groups of tally marks. We do not holdIntreferences because it would not be in the spirit of the framework.Constructors
Constructors are provided to convert
Ints andStrings toRomanNumerals.We also support conversions from
IntandStringliterals when theRomanNumeraltype can be inferred.Properties
Instance-level properties are provided to convert
RomanNumerals intoIntandStringvalues.We also provide various
*Convertibleprotocols to allow types to return differentRomanNumeralandRomanNumeralSymbolrepresentations of themselves.Arithmetic
Addition, subtraction, and multiplication operations are supported (and required) thanks to our conformance to the
Numericprotocol. We use algorithms that allow us to directly manipulate the Roman numeral symbols as opposed to doing conversions to-and-fromInts.Performance
Our committment to authenticity does have implications.
The following table compares the performance
Intarithmetic operations to Roman numeral arithmetic operations on a new MacBook Pro.IntRomanNumeralIt should be noted that this is much faster than any person from Ancient Rome could do arithmetic. Who can take issue with progress?
Copyright Text
The most useful feature we provide is automatic formatting of Copyright text.
Additive Notation
The default notation for this framework is subtractive notation - that is what instances of
RomanNumerals represent. We provide theAdditiveRomanNumeralstruct for initialization of numerals using additive notation. We also support conversions between the notations.Both notations implement the
RomanNumeralProtocolprotocol and support the same general interface.Extensions
We provide a variety of extensions on existing Swift types to make common operations easier.
Calendar&DateComponentExtensionsCalendarobjects, and theDateComponentsthey produce, are able to convert years intoRomanNumerals.Int&StringExtensionsWe conform
IntandStringto the*RomanNumeralConvertibleprotocols to complete the ouroboros with these foundational types.Authors
Kyle Hughes
Contributions
RomanNumeralKitis not accepting source contributions at this time.License
RomanNumeralKitis available under the MIT License.