Introduction
Welcome to the documentation for the MoneyFX framework. MoneyFX is a powerful financial library that provides developers with a set of tools for handling and manipulating monetary values in different currencies.
Table of Contents
Installation
To install the MoneyFX framework, follow the steps below:
- Open your terminal or command prompt.
- Navigate to your project directory.
- Run the following command to install MoneyFX via CocoaPods:
pod 'MoneyFX'
- Once the installation is complete, import the framework in your project.
import MoneyFX
Usage
Creating an Instance
To create an instance of a monetary value, you can use the following code:
// Create a MoneyFX instance representing $100
let money = MoneyFX(amount: 100, currency: .usd)
Basic Operations
MoneyFX supports basic mathematical operations such as addition, subtraction, multiplication, and division. Here are some examples:
// Adding two monetary values
let sum = money1 + money2
// Subtracting two monetary values
let difference = money1 - money2
// Multiplying a monetary value by a constant
let multipliedValue = money * 5
// Dividing a monetary value by a constant
let dividedValue = money / 2
Currency Conversion
MoneyFX allows you to convert monetary values from one currency to another. Here’s an example:
// Convert an amount from USD to EUR
let convertedValue = money.convert(to: .eur)
Formatting Currencies
MoneyFX provides convenient methods for formatting currencies according to different locales. Here’s an example:
// Format a monetary value for the current locale
let formattedValue = money.format() // Example output: $100.00
// Format a monetary value for a specific locale (e.g., French)
let formattedValueFR = money.format(locale: Locale(identifier: "fr_FR")) // Example output: 100,00 €
Advanced Operations
MoneyFX supports advanced operations such as calculating percentage changes and comparing monetary values. Here are some examples:
// Calculate the percentage change between two monetary values
let percentageChange = money1.percentageChange(to: money2)
// Compare two monetary values
if money1 > money2 {
print("Value A is greater than Value B")
} else if money1 < money2 {
print("Value A is smaller than Value B")
} else {
print("Value A is equal to Value B")
}