### Start Development Server Source: https://github.com/dinerojs/dinero.js/blob/main/examples/cart-react/README.md Start the development server for the shopping cart React example. ```sh npm run dev ``` -------------------------------- ### Install Dependencies Source: https://github.com/dinerojs/dinero.js/blob/main/examples/cart-react/README.md Install project dependencies from the repository root before navigating to the example. ```sh npm install ``` -------------------------------- ### Navigate to Example Directory Source: https://github.com/dinerojs/dinero.js/blob/main/examples/expense-splitter/README.md Change the current directory to the expense-splitter example folder. ```sh cd examples/expense-splitter ``` -------------------------------- ### Navigate to Example Directory Source: https://github.com/dinerojs/dinero.js/blob/main/examples/pricing-react/README.md Change the current directory to the pricing-react example. ```sh cd examples/pricing-react ``` -------------------------------- ### Navigate to Example Directory Source: https://github.com/dinerojs/dinero.js/blob/main/examples/portfolio-tracker/README.md Change the current directory to the portfolio-tracker example folder. ```sh cd examples/portfolio-tracker ``` -------------------------------- ### Navigate to Example Directory Source: https://github.com/dinerojs/dinero.js/blob/main/examples/invoice-builder/README.md Change the current directory to the invoice-builder example. ```sh cd examples/invoice-builder ``` -------------------------------- ### Navigate to Example Source: https://github.com/dinerojs/dinero.js/blob/main/examples/cart-react/README.md Change the current directory to the shopping cart React example. ```sh cd examples/cart-react ``` -------------------------------- ### Install Dinero.js Source: https://github.com/dinerojs/dinero.js/blob/main/README.md Install Dinero.js using npm or yarn. ```sh npm install dinero.js # or yarn add dinero.js ``` -------------------------------- ### Install Dinero Formatting Skill Source: https://github.com/dinerojs/dinero.js/blob/main/docs/agent-skills.md Install the 'dinero-formatting' skill to guide your AI agent in formatting Dinero.js amounts for display. It covers using `toDecimal`, `Intl.NumberFormat`, and `toSnapshot`. ```bash npx skills add dinerojs/skills --skill dinero-formatting ``` -------------------------------- ### Install Dinero.js Source: https://context7.com/dinerojs/dinero.js/llms.txt Install the library using npm or yarn. Import functions individually for tree-shaking. ```sh npm install dinero.js # or yarn add dinero.js ``` -------------------------------- ### Navigate to Example Directory Source: https://github.com/dinerojs/dinero.js/blob/main/examples/cart-vue/README.md Change your current directory to the specific Vue cart example. ```sh cd examples/cart-vue ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/dinerojs/dinero.js/blob/main/CONTRIBUTING.md Clone the repository, navigate to the project directory, and install Node.js dependencies using npm. Ensure you are using the correct Node.js version with nvm. ```sh git clone https://github.com/dinerojs/dinero.js.git cd dinero.js nvm use npm install ``` -------------------------------- ### Install All Dinero.js Skills Source: https://github.com/dinerojs/dinero.js/blob/main/docs/agent-skills.md Use this command to install all available Dinero.js skills for your AI coding agent. This provides a comprehensive set of guidelines for using the library. ```bash npx skills add dinerojs/skills ``` -------------------------------- ### Run Documentation Development Server Source: https://github.com/dinerojs/dinero.js/blob/main/CONTRIBUTING.md Start a local development server to preview the documentation. This is useful for making and reviewing documentation changes. ```sh npm run docs:dev ``` -------------------------------- ### Format an object in units Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/formatting/to-units.md This example demonstrates how to use toUnits to get the amount of a Dinero object in its standard units. It shows usage with and without a custom scale. ```APIDOC ## toUnits ### Description Get the amount of a Dinero object in units. ### Method `toUnits(dineroObject: Dinero, transformer?: DineroTransformer): TOutput ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Request Example ```javascript import { dinero, toUnits } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 1050, currency: USD }); const d2 = dinero({ amount: 10545, currency: USD, scale: 3 }); toUnits(d1); // [10, 50] toUnits(d2); // [10, 545] ``` ### Response #### Success Response (200) - **value** (TOutput) - The amount divided into units. #### Response Example ```json [10, 50] ``` ``` -------------------------------- ### Install Dinero Currency Patterns Skill Source: https://github.com/dinerojs/dinero.js/blob/main/docs/agent-skills.md Install the 'dinero-currency-patterns' skill to help your AI agent handle currency patterns effectively. This includes defining custom currencies, validation, and integration with payment services. ```bash npx skills add dinerojs/skills --skill dinero-currency-patterns ``` -------------------------------- ### Custom chaining implementation Source: https://github.com/dinerojs/dinero.js/blob/main/docs/faq/why-functions-instead-of-methods.md Provides an example of how to create a custom chaining wrapper for standalone functions if preferred. ```javascript function chain(d) { return { multiply: (n) => chain(multiply(d, n)), add: (other) => chain(add(d, other)), }; } chain(d1).multiply(2).add(d2); ``` -------------------------------- ### Quick Start with Dinero.js Source: https://github.com/dinerojs/dinero.js/blob/main/README.md Create Dinero objects, perform addition, and format the result to a decimal string. Requires importing specific functions and currencies. ```javascript import { dinero, add, toDecimal } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 500, currency: USD }); const d2 = dinero({ amount: 800, currency: USD }); const total = add(d1, d2); toDecimal(total); // "13.00" ``` -------------------------------- ### Install Specific Dinero.js Skill Source: https://github.com/dinerojs/dinero.js/blob/main/docs/agent-skills.md Install a particular Dinero.js skill to focus your AI agent's learning on a specific area. This is useful for targeting particular aspects of the library. ```bash npx skills add dinerojs/skills --skill dinero-best-practices ``` -------------------------------- ### Create table with MONEY type Source: https://github.com/dinerojs/dinero.js/blob/main/docs/guides/storing-in-a-database.md Example SQL statement to create a table with a `money` column. Use with caution due to limitations. ```sql CREATE TABLE products ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, price MONEY NOT NULL ); ``` -------------------------------- ### Multiply by a scaled multiplier Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/mutations/multiply.md This example shows how to multiply a Dinero object using a scaled amount, which is recommended for fractional multipliers. ```APIDOC ## multiply ### Description Multiply a Dinero object. If you need to multiply by a fractional multiplier, you shouldn't use floats, but scaled amounts instead. For example, instead of passing `2.1`, you should pass `{ amount: 21, scale: 1 }`. When using scaled amounts, the function converts the returned objects to the safest scale. ### Parameters #### Path Parameters - **multiplicand** (Dinero) - Required - The Dinero object to multiply. - **multiplier** (DineroScaledAmount) - Required - The number to multiply with. ### Request Example ```js import { dinero, multiply } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 401, currency: USD }); multiply(d, { amount: 2001, scale: 3 }); // a Dinero object with amount 802401 and scale 5 ``` ``` -------------------------------- ### Convert to a currency with a different scale Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/conversions/convert.md This example demonstrates converting between currencies with different scales. It uses a simple numeric rate and converts the result to the safest scale. ```javascript import { dinero, convert } from 'dinero.js'; import { USD, IQD } from 'dinero.js/currencies'; const rates = { IQD: 1199 }; const d = dinero({ amount: 500, currency: USD }); convert(d, IQD, rates); // a Dinero object with amount 5995000 and scale 3 ``` -------------------------------- ### Use a custom transformer Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/formatting/to-decimal.md This example shows how to use a custom transformer function with `toDecimal` to further modify the output string, such as prepending the currency code. ```APIDOC ## toDecimal with transformer ### Description Use a custom transformer function to modify the decimal output. ### Parameters - `dineroObject` (Dinero) - Required - The Dinero object to format. - `transformer` (DineroTransformer) - Optional - A function that receives the value and currency and returns a transformed string. ### Request Example ```js import { dinero, toDecimal } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 1050, currency: USD }); toDecimal(d, ({ value, currency }) => `${currency.code} ${value}`); // "USD 10.50" ``` ``` -------------------------------- ### Compare two objects with different currencies Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/comparisons/have-same-currency.md This example demonstrates how to check if Dinero objects have different currencies. Note that the example incorrectly uses `haveSameAmount` instead of `haveSameCurrency` for a false positive result. ```javascript import { dinero, haveSameAmount } from 'dinero.js'; import { USD, EUR } from 'dinero.js/currencies'; const d1 = dinero({ amount: 1000, currency: USD }); const d2 = dinero({ amount: 1000, currency: EUR }); haveSameAmount([d1, d2]); // false ``` -------------------------------- ### Multiply by an integer Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/mutations/multiply.md This example demonstrates how to multiply a Dinero object by a simple integer. ```APIDOC ## multiply ### Description Multiply a Dinero object. ### Parameters #### Path Parameters - **multiplicand** (Dinero) - Required - The Dinero object to multiply. - **multiplier** (TAmount) - Required - The number to multiply with. ### Request Example ```js import { dinero, multiply } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 400, currency: USD }); multiply(d, 4); // a Dinero object with amount 1600 ``` ``` -------------------------------- ### Format an object in decimal format Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/formatting/to-decimal.md This example demonstrates how to format a Dinero object into its decimal string representation. The output format is determined by the currency's exponent or the object's specified scale. ```APIDOC ## toDecimal ### Description Get the amount of a Dinero object in a stringified decimal representation. The number of decimal places depends on the [`scale`](/core-concepts/scale) of your object—or, when unspecified, the [`exponent`](/core-concepts/currency#currency-exponent) of its currency. ::: info You can only use this function with Dinero objects that are single-based and use a decimal currency. ::: ### Parameters - `dineroObject` (Dinero) - Required - The Dinero object to format. - `transformer` (DineroTransformer) - Optional - An optional transformer function. ### Request Example ```js import { dinero, toDecimal } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 1050, currency: USD }); const d2 = dinero({ amount: 10545, currency: USD, scale: 3 }); toDecimal(d1); // "10.50" toDecimal(d2); // "10.545" ``` ``` -------------------------------- ### Compare two objects with different amount Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/comparisons/have-same-amount.md This example demonstrates how `haveSameAmount` returns `false` when Dinero objects have different monetary values, even if they share the same currency. ```javascript import { dinero, haveSameAmount } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 1000, currency: USD }); const d2 = dinero({ amount: 2000, currency: USD }); haveSameAmount([d1, d2]); // false ``` -------------------------------- ### Create Dinero object with looked-up currency Source: https://github.com/dinerojs/dinero.js/blob/main/docs/faq/how-to-look-up-a-currency-by-code.md After looking up a currency object using its code, you can use it to create a Dinero object. This example demonstrates creating a Dinero object with a specific amount and the retrieved currency. ```typescript import { dinero } from 'dinero.js'; const code = 'USD'; const currency = getCurrency(code); const price = dinero({ amount: 5000, currency }); ``` -------------------------------- ### Calculate Percentage with Scaled Amounts Source: https://github.com/dinerojs/dinero.js/blob/main/docs/faq/can-i-multiply-by-a-decimal.md Provides an example of calculating a percentage of a Dinero object using scaled amounts. This follows the same pattern as multiplying by decimals. ```javascript multiply(d, { amount: 15, scale: 2 }); // 15 at scale 2 = 0.15 ``` -------------------------------- ### Compare Dinero objects with different scales Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/comparisons/greater-than.md This example demonstrates comparing Dinero objects that have different scales. The function automatically normalizes them to the highest scale before comparison. ```javascript import { dinero, greaterThan } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 800, currency: USD }); const d2 = dinero({ amount: 5000, currency: USD, scale: 3 }); greaterThan(d1, d2); // true ``` -------------------------------- ### Create Intl Formatter with toDecimal Source: https://github.com/dinerojs/dinero.js/blob/main/docs/getting-started/upgrade-guide.md To replicate v1.x locale-based formatting, create a custom formatter using `toDecimal` and the Internationalization API. This example shows how to create an English formatter. ```javascript import { toDecimal } from 'dinero.js'; function createIntlFormatter(locale, options = {}) { function transformer({ value, currency }) { return Number(value).toLocaleString(locale, { ...options, style: 'currency', currency: currency.code, }); } return function formatter(dineroObject) { return toDecimal(dineroObject, transformer); }; } export const intlFormat = createIntlFormatter('en-US'); ``` ```javascript import { dinero } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 500, currency: USD }); intlFormat(d); // "$5.00" ``` -------------------------------- ### Add Dinero objects with different scales Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/mutations/add.md This example demonstrates adding Dinero objects that have different scales. The `add` function automatically normalizes them to the highest scale before summing. ```javascript import { dinero, add } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 400, currency: USD }); const d2 = dinero({ amount: 104545, currency: USD, scale: 4 }); add(d1, d2); // a Dinero object with amount 144545 and scale 4 ``` -------------------------------- ### Database Persistence Patterns for Dinero.js Source: https://context7.com/dinerojs/dinero.js/llms.txt Store and retrieve Dinero objects reliably in SQL and NoSQL databases. Use `toSnapshot` to get a serializable representation and reconstruct Dinero objects from stored data. ```javascript import { dinero, toSnapshot, toDecimal } from 'dinero.js'; import { EUR } from 'dinero.js/currencies'; const price = dinero({ amount: 6999, currency: EUR }); // €69.99 // ── SQL: separate columns ────────────────────────────────────────────────── // CREATE TABLE products ( // price_amount BIGINT NOT NULL, // price_currency VARCHAR(3) NOT NULL, // price_exponent INTEGER NOT NULL DEFAULT 2 // ); const { amount, currency, scale } = toSnapshot(price); // INSERT: (amount=6999, currency='EUR', exponent=2) const row = { price_amount: 6999, price_currency: 'EUR', price_exponent: 2 }; const restored = dinero({ amount: row.price_amount, currency: { code: row.price_currency, base: 10, exponent: row.price_exponent }, }); // ── SQL: JSON column ─────────────────────────────────────────────────────── await db.query( 'INSERT INTO products (name, price) VALUES ($1, $2)', ['Mass Effect: Legendary Edition', JSON.stringify(toSnapshot(price))] ); const { rows } = await db.query('SELECT * FROM products WHERE id = $1', [1]); const product = { ...rows[0], price: dinero(rows[0].price) }; // ── MongoDB ──────────────────────────────────────────────────────────────── await collection.insertOne({ name: 'Game', price: toSnapshot(price) }); const doc = await collection.findOne({ name: 'Game' }); const game = { ...doc, price: dinero(doc.price) }; ``` -------------------------------- ### Create a Factory for Float Inputs Source: https://github.com/dinerojs/dinero.js/blob/main/docs/guides/creating-from-floats.md Use this helper function when your amounts are in float format (e.g., 19.99) and you need to convert them to minor currency units before creating a Dinero object. This code is a starting point and may need adjustments for edge cases. ```javascript function dineroFromFloat({ amount: float, currency, scale }) { const factor = currency.base ** (scale ?? currency.exponent); const amount = Math.round(float * factor); return dinero({ amount, currency, scale }); } ``` -------------------------------- ### Check an object with sub-units based on scale Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/comparisons/has-sub-units.md This example shows how `hasSubUnits` returns true when a Dinero object is created with an explicit scale, even if the currency might not typically have sub-units. Imports for `dinero` and `hasSubUnits` are necessary. ```javascript import { dinero, hasSubUnits } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 1100, currency: USD, scale: 3 }); hasSubUnits(d); // true ``` -------------------------------- ### Get the greatest object from a set after normalization Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/comparisons/maximum.md This example demonstrates how 'maximum' handles Dinero objects with different scales but the same currency. The function normalizes them to the highest scale before comparison. Imports are the same as for basic comparison. ```javascript import { dinero, maximum } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 500, currency: USD }); const d2 = dinero({ amount: 1000, currency: USD, scale: 3 }); maximum([d1, d2]); // a Dinero object with amount 5000 and scale 3 ``` -------------------------------- ### Check Bundle Sizes Source: https://github.com/dinerojs/dinero.js/blob/main/CLAUDE.md Verify that the bundle sizes of the project adhere to predefined limits. This command helps in monitoring and controlling the size of the library. ```bash npm run test:size ``` -------------------------------- ### Define Currency Base (Non-Decimal) Source: https://github.com/dinerojs/dinero.js/blob/main/docs/core-concepts/currency.md For non-decimal currencies, the base can be a number other than 10. For example, the Mauritanian ouguiya has a base of 5. ```javascript // Mauritanian ouguiya const MRU = { code: 'MRU', base: 5, // ... }; ``` -------------------------------- ### Build a reusable converter Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/conversions/convert.md Shows how to create a reusable converter function by predefining exchange rates. ```APIDOC ## Create Reusable Converter ### Description Creates a higher-order function that returns a reusable converter function, pre-configured with specific exchange rates. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Method `createConverter(rates)` ### Parameters - **rates** (`DineroRates`) - Required - The exchange rates to use for the converter. ### Returns - `function(dineroObject, newCurrency)` - A converter function that takes a Dinero object and a target currency, and returns the converted Dinero object. ### Request Example ```js import { dinero, convert } from 'dinero.js'; import { USD, EUR } from 'dinero.js/currencies'; const rates = { EUR: { amount: 89, scale: 2 } }; function createConverter(rates) { return function converter(dineroObject, newCurrency) { return convert(dineroObject, newCurrency, rates); }; } const converter = createConverter(rates); const d = dinero({ amount: 500, currency: USD }); converter(d, EUR); // a Dinero object with amount 44500 and scale 4 ``` ``` -------------------------------- ### Using BigInt Currencies with Dinero.js Source: https://github.com/dinerojs/dinero.js/blob/main/docs/faq/why-cant-i-use-currencies-with-bigint.md Illustrates how to import and use currencies from `dinero.js/bigint/currencies` when working with the bigint variant of Dinero.js. Ensure you import `dinero` from the bigint path as well. ```javascript import { dinero } from 'dinero.js/bigint'; import { USD } from 'dinero.js/bigint/currencies'; const d = dinero({ amount: 500n, currency: USD }); ``` -------------------------------- ### Create a custom Dinero instance with a calculator Source: https://github.com/dinerojs/dinero.js/blob/main/docs/guides/precision-and-large-numbers.md After defining a custom calculator, use `createDinero` to build a Dinero instance that utilizes your custom amount type and calculator logic. ```javascript import { createDinero } from 'dinero.js'; // ... const bigDinero = createDinero({ calculator }); ``` -------------------------------- ### Import Dinero.js core and currencies for bigint Source: https://github.com/dinerojs/dinero.js/blob/main/docs/getting-started/quick-start.md Import core functions and currency definitions for `bigint` amounts when dealing with very large numbers. ```js // Standard usage import { dinero, add, subtract } from 'dinero.js'; import { USD, EUR } from 'dinero.js/currencies'; // For large amounts (bigint) import { dinero } from 'dinero.js/bigint'; import { USD, EUR } from 'dinero.js/bigint/currencies'; ``` -------------------------------- ### Update currency handling from v1.x Source: https://github.com/dinerojs/dinero.js/blob/main/docs/getting-started/upgrade-guide.md v2 requires currency to be an object, not a string. Import ISO 4217 currency objects from `dinero.js/currencies`. ```diff ```diff - Dinero({ amount: 500, currency: 'USD' }); + import { USD } from 'dinero.js/currencies'; + dinero({ amount: 500, currency: USD }); ``` ``` -------------------------------- ### Get the lowest object from a set Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/comparisons/minimum.md Compares Dinero objects with the same currency to find the minimum value. Ensure all objects share the same currency before calling. ```javascript import { dinero, minimum } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 150, currency: USD }); const d2 = dinero({ amount: 50, currency: USD }); minimum([d1, d2]); // a Dinero object with amount 50 ``` -------------------------------- ### Create Products Table with Separate Price Components Source: https://github.com/dinerojs/dinero.js/blob/main/docs/guides/storing-in-a-database.md Defines a SQL table to store product information, including price amount, currency code, and exponent in separate columns. This approach is flexible and database-agnostic. ```sql CREATE TABLE products ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, price_amount BIGINT NOT NULL, price_currency VARCHAR(3) NOT NULL, price_exponent INTEGER NOT NULL DEFAULT 2 ); INSERT INTO products (name, price_amount, price_currency, price_exponent) VALUES ('Mass Effect: Legendary Edition', 6999, 'EUR', 2); ``` -------------------------------- ### maximum Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/comparisons/maximum.md Get the greatest of the passed Dinero objects. You can only compare objects that share the same currency. The function also normalizes objects to the same scale (the highest) before comparing them. ```APIDOC ## maximum ### Description Get the greatest of the passed Dinero objects. You can only compare objects that share the same currency. The function also normalizes objects to the same scale (the highest) before comparing them. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters #### `dineroObjects` - **Type**: `Dinero[]` - **Description**: The Dinero objects to maximum. - **Required**: Yes ### Request Example None ### Response #### Success Response (200) - **Type**: `Dinero` - **Description**: The greatest Dinero object from the provided set. ### Response Example None ### Code examples #### Get the greatest object from a set ```js import { dinero, maximum } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 150, currency: USD }); const d2 = dinero({ amount: 50, currency: USD }); maximum([d1, d2]); // a Dinero object with amount 150 ``` #### Get the greatest object from a set after normalization ```js import { dinero, maximum } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 500, currency: USD }); const d2 = dinero({ amount: 1000, currency: USD, scale: 3 }); maximum([d1, d2]); // a Dinero object with amount 5000 and scale 3 ``` ``` -------------------------------- ### minimum Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/comparisons/minimum.md Get the lowest of the passed Dinero objects. You can only compare objects that share the same currency. The function also normalizes objects to the same scale (the highest) before comparing them. ```APIDOC ## minimum ### Description Get the lowest of the passed Dinero objects. You can only compare objects that share the same currency. The function also normalizes objects to the same scale (the highest) before comparing them. ### Parameters #### Path Parameters - `dineroObjects` (Dinero[]) - Required - The Dinero objects to minimum. ### Code examples #### Get the lowest object from a set ```js import { dinero, minimum } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 150, currency: USD }); const d2 = dinero({ amount: 50, currency: USD }); minimum([d1, d2]); // a Dinero object with amount 50 ``` #### Get the lowest object from a set after normalization ```js import { dinero, minimum } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 500, currency: USD }); const d2 = dinero({ amount: 1000, currency: USD, scale: 3 }); minimum([d1, d2]); // a Dinero object with amount 1000 and scale 3 ``` ``` -------------------------------- ### Get the lowest object from a set after normalization Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/comparisons/minimum.md Compares Dinero objects with different scales but the same currency. The function automatically normalizes them to the highest scale before comparison. ```javascript import { dinero, minimum } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 500, currency: USD }); const d2 = dinero({ amount: 1000, currency: USD, scale: 3 }); minimum([d1, d2]); // a Dinero object with amount 1000 and scale 3 ``` -------------------------------- ### Usage with transformScale Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/rounding/half-odd.md Demonstrates how to use the `halfOdd` rounding mode with the `transformScale` function. ```APIDOC ## Use with transformScale Pass this function as the last argument to [`transformScale`](/api/conversions/transform-scale) to control how remainders are handled. ### Request Example ```javascript import { dinero, transformScale, halfOdd } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 1050, currency: USD, scale: 3 }); transformScale(d, 2, halfOdd); // a Dinero object with amount 105 and scale 2 ``` ``` -------------------------------- ### Run Project Tests Source: https://github.com/dinerojs/dinero.js/blob/main/CONTRIBUTING.md Execute all project tests using npm. This command is essential to ensure code quality and functionality before committing changes. ```sh npm test ``` -------------------------------- ### Use with transformScale Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/rounding/half-down.md Demonstrates how to use the `halfDown` rounding mode with the `transformScale` function. ```APIDOC ## Use with transformScale Pass this function as the last argument to [`transformScale`](/api/conversions/transform-scale) to control how remainders are handled. ### Code example ```javascript import { dinero, transformScale, halfDown } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 1055, currency: USD, scale: 3 }); transformScale(d, 2, halfDown); // a Dinero object with amount 105 and scale 2 ``` ``` -------------------------------- ### Get the Maximum Value from Dinero Objects Source: https://context7.com/dinerojs/dinero.js/llms.txt Use `maximum` to retrieve the Dinero object with the highest value from an array. Ensure all Dinero objects in the array have the same currency. ```javascript import { dinero, maximum, toSnapshot } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 500, currency: USD }); const d2 = dinero({ amount: 200, currency: USD }); const d3 = dinero({ amount: 800, currency: USD }); toSnapshot(maximum([d1, d2, d3])); // { amount: 800 } → $8.00 ``` -------------------------------- ### Get Default Scale - Dinero.js Source: https://github.com/dinerojs/dinero.js/blob/main/docs/core-concepts/scale.md Demonstrates how the scale defaults to the currency exponent when creating a Dinero object without explicitly providing a scale. This is the most common scenario. ```javascript import { dinero, toSnapshot } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 5000, currency: USD }); const { exponent } = USD; // `exponent` is 2 const { scale } = toSnapshot(d); // `scale` is 2 (picked up `USD.exponent`) ``` -------------------------------- ### Instantiate Dinero with Integer Amount Source: https://github.com/dinerojs/dinero.js/blob/main/docs/guides/creating-from-floats.md Use this when you have amounts already in minor currency units (integers). ```javascript import { dinero, add, subtract } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 1999, currency: USD }); ``` -------------------------------- ### Chainable method usage (alternative) Source: https://github.com/dinerojs/dinero.js/blob/main/docs/faq/why-functions-instead-of-methods.md Illustrates the alternative approach using chainable methods, which Dinero.js avoids. ```javascript d1.add(d2); ``` -------------------------------- ### Get a snapshot of a Dinero object Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/formatting/to-snapshot.md Use toSnapshot to convert a Dinero object into a plain JavaScript object. This is useful for serializing Dinero objects for storage or transmission. ```javascript import { dinero, toSnapshot } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 500, currency: USD }); toSnapshot(d); // { // amount: 500, // currency: { // code: 'USD', // base: 10, // exponent: 2, // }, // scale: 2, // } ``` -------------------------------- ### CDN Usage Source: https://context7.com/dinerojs/dinero.js/llms.txt Include Dinero.js via CDN for UMD builds. Access functions through the window object. ```html ``` -------------------------------- ### Check if Dinero object value is negative Source: https://context7.com/dinerojs/dinero.js/llms.txt Returns true if the Dinero object's amount is less than 0. No specific setup is needed, just import the function. ```javascript import { dinero, isNegative } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; isNegative(dinero({ amount: -100, currency: USD })); // true isNegative(dinero({ amount: 0, currency: USD })); // false isNegative(dinero({ amount: 500, currency: USD })); // false ``` -------------------------------- ### Create a Dinero object Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/dinero.md Create a Dinero object by providing the amount in minor currency units and the currency object. The scale defaults to the currency's exponent. ```APIDOC ## Create a Dinero object ### Description Create a Dinero object that represents a monetary value. You specify the amount in minor currency units (e.g., cents for the US dollar) and pass a currency. The scale defaults to the currency's exponent but can be set manually for additional precision. ### Parameters #### Request Body - **amount** (TAmount) - Required - The amount in minor currency units. Must be an integer. - **currency** (DineroCurrency) - Required - The currency object. - **scale** (TAmount) - Optional - The number of decimal places to represent. Defaults to the currency exponent. ### Request Example ```js import { dinero } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; // This represents $5.00 const d = dinero({ amount: 500, currency: USD }); ``` ``` -------------------------------- ### Use with multiply Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/rounding/half-down.md Demonstrates how to use the `halfDown` rounding mode with the `multiply` function. ```APIDOC ## Use with multiply Pass this function as the last argument to [`multiply`](/api/mutations/multiply) to control how remainders are handled. ### Code example ```javascript import { dinero, multiply, halfDown } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 305, currency: USD }); multiply(d, { amount: 21, scale: 1 }, halfDown); // a Dinero object with amount 6405 and scale 3 ``` ``` -------------------------------- ### Use halfAwayFromZero with multiply Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/rounding/half-away-from-zero.md Pass halfAwayFromZero as the last argument to the multiply function to control how remainders are handled. This example demonstrates multiplying a Dinero object and rounding the result. ```javascript import { dinero, multiply, halfAwayFromZero } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 305, currency: USD }); multiply(d, { amount: 21, scale: 1 }, halfAwayFromZero); // a Dinero object with amount 6405 and scale 3 ``` -------------------------------- ### Check if Dinero object value is positive Source: https://context7.com/dinerojs/dinero.js/llms.txt Returns true if the Dinero object's amount is greater than 0. Does not require any special setup beyond importing the function. ```javascript import { dinero, isPositive } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; isPositive(dinero({ amount: 500, currency: USD })); // true isPositive(dinero({ amount: 0, currency: USD })); // false isPositive(dinero({ amount: -100, currency: USD })); // false ``` -------------------------------- ### Convert to another currency Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/conversions/convert.md Demonstrates how to convert a Dinero object to a different currency using provided exchange rates. ```APIDOC ## Convert Dinero Object ### Description Converts a Dinero object from its current currency to a new currency using specified exchange rates. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Method `convert(dineroObject, newCurrency, rates)` ### Parameters - **dineroObject** (`Dinero`) - Required - The Dinero object to convert. - **newCurrency** (`DineroCurrency`) - Required - The target currency. - **rates** (`DineroRates`) - Required - The exchange rates to use for conversion. Can be a number or an object with `amount` and `scale` for fractional rates. ### Request Example ```js import { dinero, convert } from 'dinero.js'; import { USD, EUR } from 'dinero.js/currencies'; const rates = { EUR: { amount: 89, scale: 2 } }; const d = dinero({ amount: 500, currency: USD }); convert(d, EUR, rates); // a Dinero object with amount 44500 and scale 4 ``` ### Response #### Success Response (Dinero object) - **amount** (number) - The converted amount. - **currency** (object) - The new currency object. - **scale** (number) - The scale of the converted amount. #### Response Example ```json { "amount": 44500, "currency": { "code": "EUR", "base": 10, "exponent": 2 }, "scale": 4 } ``` ### Notes - Currencies must share the same base. Converting between currencies with different bases will throw an error. - For fractional rates, use scaled amounts (e.g., `{ amount: 89, scale: 2 }`) instead of floats. ``` -------------------------------- ### Use halfAwayFromZero with transformScale Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/rounding/half-away-from-zero.md Pass halfAwayFromZero as the last argument to the transformScale function to control how remainders are handled. This example demonstrates transforming the scale of a Dinero object and rounding the result. ```javascript import { dinero, transformScale, halfAwayFromZero } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 1055, currency: USD, scale: 3 }); transformScale(d, 2, halfAwayFromZero); // a Dinero object with amount 106 and scale 2 ``` -------------------------------- ### Import and Use Built-in Currencies (Standard) Source: https://github.com/dinerojs/dinero.js/blob/main/docs/core-concepts/currency.md Dinero.js provides ISO 4217 currency objects. Import them from 'dinero.js/currencies' and pass them to the Dinero constructor. ```javascript import { dinero } from 'dinero.js'; import { USD, EUR } from 'dinero.js/currencies'; const d1 = dinero({ amount: 1000, currency: USD }); const d2 = dinero({ amount: 1000, currency: EUR }); ``` -------------------------------- ### Get the Minimum Value from Dinero Objects Source: https://context7.com/dinerojs/dinero.js/llms.txt The `minimum` function finds and returns the Dinero object with the lowest value from a given array. All objects in the array must share the same currency. ```javascript import { dinero, minimum, toSnapshot } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d1 = dinero({ amount: 500, currency: USD }); const d2 = dinero({ amount: 200, currency: USD }); const d3 = dinero({ amount: 800, currency: USD }); toSnapshot(minimum([d1, d2, d3])); // { amount: 200 } → $2.00 ``` -------------------------------- ### Composition with functional utilities Source: https://github.com/dinerojs/dinero.js/blob/main/docs/faq/why-functions-instead-of-methods.md Shows how standalone functions can be composed using functional utilities like Ramda's pipe. ```javascript import { pipe } from 'ramda'; pipe( (d) => multiply(d, 2), (d) => add(d, fee), (d) => toDecimal(d), )(price); ``` -------------------------------- ### Replace getAmount, getCurrency, getPrecision with toSnapshot Source: https://github.com/dinerojs/dinero.js/blob/main/docs/getting-started/upgrade-guide.md Use `toSnapshot` to get a plain object containing amount, scale, and currency. This replaces the individual `getAmount`, `getCurrency`, and `getPrecision` methods. ```diff - const amount = Dinero({ amount: 500, currency: 'USD' }).getAmount(); - const currency = Dinero({ amount: 500, currency: 'USD' }).getCurrency(); - const scale = Dinero({ amount: 500, currency: 'USD' }).getPrecision(); + const { amount, scale, currency } = toSnapshot( + dinero({ amount: 500, currency: USD }) + ); ``` -------------------------------- ### Define Currency Exponent (Standard) Source: https://github.com/dinerojs/dinero.js/blob/main/docs/core-concepts/currency.md The currency exponent expresses the decimal relationship between the currency and its minor unit. For example, 100 cents in a US dollar means an exponent of 2 (10^2). ```javascript const USD = { code: 'USD', base: 10, exponent: 2, }; ``` -------------------------------- ### Create Dinero Object (Number-backed) Source: https://context7.com/dinerojs/dinero.js/llms.txt Create Dinero objects with amounts in minor currency units. The scale defaults to the currency's exponent but can be overridden. ```js import { dinero, toSnapshot } from 'dinero.js'; import { USD, JPY, EUR } from 'dinero.js/currencies'; // $50.00 — amount in cents const price = dinero({ amount: 5000, currency: USD }); // ¥5000 — JPY has exponent 0, so amount is in major units const jpy = dinero({ amount: 5000, currency: JPY }); // €0.035 — explicit scale overrides exponent const precise = dinero({ amount: 35, currency: EUR, scale: 3 }); toSnapshot(price); // { amount: 5000, currency: { code: 'USD', base: 10, exponent: 2 }, scale: 2 } toSnapshot(precise); // { amount: 35, currency: { code: 'EUR', base: 10, exponent: 2 }, scale: 3 } ``` -------------------------------- ### Calculate checkout totals Source: https://github.com/dinerojs/dinero.js/blob/main/docs/core-concepts/mutations.md Demonstrates calculating subtotals, applying discounts, and adding shipping costs using `add`, `allocate`, and `subtract` functions. This is useful for e-commerce applications. ```javascript import { dinero, add, allocate, subtract } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const products = [ { name: 'Apple iPhone 12', price: dinero({ amount: 89900, currency: USD }), }, { name: 'Apple AirPods Pro', price: dinero({ amount: 17495, currency: USD }), }, ]; const subtotal = products.reduce( (acc, { price }) => add(acc, price), dinero({ amount: 0, currency: USD }) ); const [discount] = allocate(subtotal, [20, 80]); const discounted = subtract(subtotal, discount); const shipping = dinero({ amount: 1000, currency: USD }); const total = add(discounted, shipping); ``` -------------------------------- ### toSnapshot Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/formatting/to-snapshot.md Get a snapshot of a Dinero object. Snapshots are plain JavaScript objects, suited for transport and storage. They're also useful when you need to retrieve raw data from a Dinero object. ```APIDOC ## toSnapshot ### Description Get a snapshot of a Dinero object. Snapshots are plain JavaScript objects, suited for transport and storage. They're also useful when you need to retrieve raw data from a Dinero object. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Method Signature `toSnapshot(dineroObject: Dinero): DineroSnapshot` ### Parameters #### Parameters - **dineroObject** (Dinero) - Required - The Dinero object to snapshot. ### Request Example ```javascript import { dinero, toSnapshot } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; const d = dinero({ amount: 500, currency: USD }); toSnapshot(d); ``` ### Response #### Success Response - **amount** (number) - The monetary amount. - **currency** (object) - The currency object. - **code** (string) - The currency code. - **base** (number) - The base of the currency. - **exponent** (number) - The exponent of the currency. - **scale** (number) - The scale of the amount. #### Response Example ```json { "amount": 500, "currency": { "code": "USD", "base": 10, "exponent": 2 }, "scale": 2 } ``` ``` -------------------------------- ### Format a non-decimal object Source: https://github.com/dinerojs/dinero.js/blob/main/docs/api/formatting/to-units.md This example demonstrates how to use toUnits with a custom currency that has a non-decimal base. The function correctly calculates the units based on the specified base, returning an array of amounts for each subdivision. ```javascript import { dinero, toUnits } from 'dinero.js'; const GRD = { code: 'GRD', base: 6, exponent: 1 }; const d = dinero({ amount: 9, currency: GRD }); toUnits(d); // [1, 3] ``` -------------------------------- ### Implement a custom Dinero.js calculator with big.js Source: https://github.com/dinerojs/dinero.js/blob/main/docs/guides/precision-and-large-numbers.md Create a custom calculator conforming to the `DineroCalculator` interface to integrate Dinero.js with third-party arbitrary-precision libraries like big.js. ```typescript import Big from 'big.js'; import { DineroCalculator, DineroComparisonOperator } from 'dinero.js'; const calculator: DineroCalculator = { add: (a, b) => a.plus(b), compare: (a, b) => a.cmp(b) as unknown as DineroComparisonOperator, decrement: (v) => v.minus(new Big(1)), increment: (v) => v.plus(new Big(1)), integerDivide: (a, b) => a.div(b).round(0, Big.roundDown), modulo: (a, b) => a.mod(b), multiply: (a, b) => a.times(b), power: (a, b) => a.pow(Number(b)), subtract: (a, b) => a.minus(b), zero: () => new Big(0), }; ``` -------------------------------- ### Import core functions and currencies Source: https://github.com/dinerojs/dinero.js/blob/main/docs/getting-started/quick-start.md Import the necessary functions and currency definitions from the Dinero.js library for standard usage. ```js import { dinero, add } from 'dinero.js'; import { USD } from 'dinero.js/currencies'; ```