CurrencyFormat extends Helper
Currency Format Helper
Provides conversion routines to and from currency strings and float values. Uses Blesta's Currencies model which provides automatic caching for increased performance.
Table of Contents
Properties
| $components | |
| $helpers | |
| $models | |
| $company_id |
Methods
| __construct() | Construct a new CurrencyFormat object |
| cast() | Casts the currency string into a float value. Conversions are cached to provide increased performance. To clear cache see CurrencyFormat::clear() |
| clear() | Clears the currency format cache. Useful if you made modifications to the currency settings and need to continue conversion using those new settings. |
| format() | Formats the float value into a currency string. Conversions are cached to provide increased performance. To clear cache see CurrencyFormat::clear() |
| setCompany() | Set the company ID to work off of |
| truncateDecimal() | Removes trailing zeros from a string preserving $min decimal places |
Methods
__construct()
Construct a new CurrencyFormat object
public
__construct([int $company_id = null ]) : mixed
Parameters
- $company_id : int = null
-
The ID of the company to work off of
Tags
cast()
Casts the currency string into a float value. Conversions are cached to provide increased performance. To clear cache see CurrencyFormat::clear()
public
cast(string $value, string $currency[, int $decimals = null ]) : string
Parameters
- $value : string
-
A currency value
- $currency : string
-
The ISO 4217 currency code format representing $value
- $decimals : int = null
-
The number of decimal places (precision) (optional, default null to use the currency's defined precision)
Tags
Return values
string —The value in decimal format based on ISO 31-0
clear()
Clears the currency format cache. Useful if you made modifications to the currency settings and need to continue conversion using those new settings.
public
clear() : mixed
format()
Formats the float value into a currency string. Conversions are cached to provide increased performance. To clear cache see CurrencyFormat::clear()
public
format(float $value, string $currency[, array<string|int, mixed> $options = null ]) : string
Parameters
- $value : float
-
The decimal value to convert
- $currency : string
-
The ISO 4217 currency code format to apply to $value
- $options : array<string|int, mixed> = null
-
An array of options to control the format (optional), if not set the options are pulled from the company settings. Options include:
- prefix Whether or not to include the prefix symbol (if one exists) true/false default true
- suffix Whether or not to include the suffix symbold (if one exists) true/false default true
- code Whether or not to include the currency code in the result (if one exists) true/false default company setting
- with_separator Whether or not to include the separator (, or sometimes . or space) in the result true/false default true
- html_code Whether or not to wrap the currency code with HTML true/false (default false). If true, will produce currency code like USD
- decimals The number of decimal places (precision) (optional, default null to use the currency's defined precision)
- significant_decimals True to include only the significant decimals (non-zero), regardless of the given decimals precision (optional, false by default)
Tags
Return values
string —The formatted value
setCompany()
Set the company ID to work off of
public
setCompany(int $company_id) : int
Parameters
- $company_id : int
-
The ID of the company to work off of
Return values
int —Returns the ID of the previously set company ID (null of none set)
truncateDecimal()
Removes trailing zeros from a string preserving $min decimal places
public
truncateDecimal(string $value, int $min[, char $decimal_char = '.' ]) : mixed
Parameters
- $value : string
-
The decimal value in string format
- $min : int
-
The minimum number of decimal places
- $decimal_char : char = '.'
-
The character used in $value for decimal representation