# easy-invoice/2.4.0/includes/Helpers/MoneyHelper.php

Easy Invoice – Invoice Generator, PDF Quotes &amp; Payments, version 2.4.0. 88 lines.

- Page: https://pluginprobe.com/plugins/easy-invoice/2.4.0/code/includes/Helpers/MoneyHelper.php
- Raw: https://pluginprobe.com/plugins/easy-invoice/2.4.0/raw/includes/Helpers/MoneyHelper.php
- Modified: 2026-09-15T12:31:20+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/easy-invoice/2.4.0/code/includes/Helpers/MoneyHelper.php#L10-L20`.

```php
<?php
/**
 * Money formatting helpers for templates.
 *
 * One way to print an amount everywhere — symbol or code, its position,
 * separators and precision from Settings → Currency, or from the document
 * when it carries its own currency.
 *
 * @package EasyInvoice
 * @since 2.4.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'easy_invoice_format_money' ) ) {
	/**
	 * Format an amount in the document's currency, or the site's.
	 *
	 * @param float|int|string $amount   Amount.
	 * @param object|null      $document Invoice or quote model, when there is one.
	 * @return string
	 */
	function easy_invoice_format_money( $amount, $document = null, string $currency_code = '' ): string {
		if ( '' !== $currency_code && ! $document ) {
			// A bare currency (a dashboard total in EUR on a USD site): the
			// formatter reads the code and position off a document, so hand
			// it a stand-in carrying just those.
			$document = new class( strtoupper( $currency_code ) ) {
				/** @var string */
				private $code;
				public function __construct( string $code ) { $this->code = $code; }
				public function getCurrencyCode(): string { return $this->code; }
				public function getCurrencyPosition(): string { return 'global'; }
			};
		}
		$formatter = ( $document && is_callable( [ $document, 'getTotal' ] ) && strpos( get_class( $document ), 'Quote' ) !== false )
			? new \EasyInvoice\Helpers\QuoteFormatter( $document )
			: new \EasyInvoice\Helpers\InvoiceFormatter( $document );
		return (string) $formatter->format( (float) $amount );
	}
}

if ( ! function_exists( 'easy_invoice_money_format_spec' ) ) {
	/**
	 * The pieces a script needs to format money the same way as PHP.
	 *
	 * @param object|null $document Invoice model, when there is one.
	 * @return array{symbol:string,position:string,thousands:string,decimal:string,precision:int}
	 */
	function easy_invoice_money_format_spec( $document = null ): array {
		$code     = $document && is_callable( [ $document, 'getCurrencyCode' ] ) ? (string) $document->getCurrencyCode() : '';
		$position = $document && is_callable( [ $document, 'getCurrencyPosition' ] ) ? (string) $document->getCurrencyPosition() : '';
		if ( '' === $code || 'global' === $code ) {
			$code = (string) get_option( 'easy_invoice_currency_code', 'USD' );
		}
		if ( '' === $position || 'global' === $position ) {
			$position = (string) get_option( 'easy_invoice_currency_position', 'before' );
		}
		$code   = strtoupper( $code );
		$symbol = 'code' === get_option( 'easy_invoice_currency_symbol_type', 'symbol' )
			? $code
			: \EasyInvoice\Helpers\CurrencyHelper::getCurrencySymbol( $code );
		return [
			'symbol'    => $symbol,
			'position'  => $position,
			'thousands' => (string) get_option( 'easy_invoice_thousands_separator', ',' ),
			'decimal'   => (string) get_option( 'easy_invoice_decimal_separator', '.' ),
			'precision' => (int) get_option( 'easy_invoice_decimal_precision', 2 ),
		];
	}
}

if ( ! function_exists( 'easy_invoice_round_money' ) ) {
	/**
	 * Round a money amount to cents, half away from zero — the same rule
	 * the builder's JavaScript uses, so a line, its subtotal and the
	 * total printed on the document always add up.
	 *
	 * @param float|int|string $amount
	 * @return float
	 */
	function easy_invoice_round_money( $amount ): float {
		return round( (float) $amount, 2 );
	}
}

```
