Appearance
money.ts Functions
The money utility functions are used to convert currency amounts to integers and vice versa, and to format currency amounts. These functions are used across the application to perform common tasks such as converting currency, formatting dates, and more.
The reason for using integers to represent currency/monetary value is to avoid floating point arithmetic issues.
INFO
Can be found in the utils directory.
Here are some folder paths where you can find the money.ts file:
utils/money.tsservices/utils/money.ts
amountToInteger
Converts a currency amount to an integer.
Parameters:
amount: A number representing the currency amount with decimal places.
Returns:
- An integer representation of the currency amount.
Example:
typescript
const integerAmount = amountToInteger(120.30); // Returns 12030integerToAmount
Converts an integer to a currency amount with two decimal places.
Parameters:
intAmount: An integer representing the value without decimal places.
Returns:
- A number representing the currency amount with two decimal places.
Example:
typescript
const amount = integerToAmount(12030); // Returns 120.30currencyToAmount
Converts a string representation of currency to a number.
Parameters:
str: A string representing the currency amount.numberFormatRegex: A regular expression used to format the number.separator: A string used as the decimal separator.
Returns:
- If the
strcan be parsed into a number, it returns that number. - If the
strcannot be parsed into a number, it returnsnull.
Example:
typescript
const amount = currencyToAmount("$120.30"); // Returns 120.30currencyToInteger
Converts a currency string to an integer.
Parameters:
str: A string representing the currency amount.
Returns:
- If the
strcan be parsed into a number, it returns that number multiplied by 100 (to convert it to an integer). - If the
strcannot be parsed into a number, it returnsnull.
Example:
typescript
const integer = currencyToInteger("$125"); // Returns 12500amountToCurrency
Formats an amount to a currency depending on locale.
Parameters:
n: A number representing the currency amount.locale: A string representing the locale. Default is 'en-US'.
Returns:
- A string representing the formatted currency amount.
Example:
typescript
const currency = amountToCurrency(12500.50); // Returns "12,500.50"integerToCurrency
Formats an integer to a currency depending on locale.
Parameters:
n: An integer representing the value without decimal places.locale: A string representing the locale. Default is 'en-US'.
Returns:
- A string representing the formatted currency amount.
Example:
typescript
const currency = integerToCurrency(157999); // Returns "1,579.99"stringToInteger
Converts a string to an integer.
Parameters:
str: A string.
Returns:
- If the
strcan be parsed into an integer, it returns that integer. - If the
strcannot be parsed into an integer, it returnsnull.
Example:
typescript
const integer = stringToInteger("125"); // Returns 125Please note that the exact behavior of these functions may vary depending on the implementation of numberFormatRegex, separator, and amountToInteger, which are not shown in the provided code snippet.