Fixed Function currencyFormat bug

topkurs2

Active member
Affected version
2.2.12
Hello.
The issue was discussing here.
XF\Language::currencyFormat method now returns only fixed (two) decimal places despite the argument passed. Now it's impossible to format this value and change into custom view. I.e. 0.12345. It worked fine till late XF 2.1.x versions. Than in last 2.2.x versions seems something changes and this function stopped work correctly.

Where is the problem?
Language.php, function currencyFormat.

In XF 2.1.2 it was
PHP:
	public function currencyFormat($value, $symbol, $precision = 2, $format = null)
	{
		$format = $format ?: $this->options['currency_format'];
		return strtr($format, [
			'{symbol}' => $symbol,
			'{value}' => $this->numberFormat($value, $precision)
		]);
	}
In XF 2.2.12 became
PHP:
		public function currencyFormat($value, $symbol, $desiredPrecision = 2, $format = null): string
	{
		if ($value === null)
		{
			$value = 0;
		}

		if (!is_integer($value) && floor($value) !== $value)
		{
			// don't hide a decimal if we have one
			$desiredPrecision = 2;
		}

		$format = $format ?: $this->options['currency_format'];
		return strtr($format, [
			'{symbol}' => $symbol,
			'{value}' => $this->numberFormat($value, $desiredPrecision)
		]);
	}
This one
PHP:
if (!is_integer($value) && floor($value) !== $value)
		{
			// don't hide a decimal if we have one
			$desiredPrecision = 2;
		}

Why it should be fixed as was before?
1. Even XF\Data\Currency currencies not dispayed correctly. For example VND (Vietnamese Dong).

2. Incorrect use of some currencies, because there is a sufficient list of 1/1000 currencies - Bahraini dinar, Kuwaiti dinar, Omani rial, Tunisian dinar, Japanese rin and so on https://en.wikipedia.org/wiki/List_of_circulating_currencies

3. Now it's impossible to use cryptocurrency. Bitcoin, Ethereum and so on. Min available value now is 0.01, 0.01 bitcoin equals 220$ (unrealistic value for everyday use). No ability to format this value into custom view.

I suggest to return as it was before. Or to fix this bug.

Thanks!
 
Last edited:
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.2.13).

Change log:
currencyFormat was changed to allow figures with no decimals to show without decimal places, but to show the decimals if any were present. In doing so, we managed to kill the ability to specify the number of decimals. Oopsie. So now you can do that again, and you can also now specify `-1` precision in order to prevent number_format from limiting or artificially extending the decimal places at all.
There may be a delay before changes are rolled out to the XenForo Community.
 
Top Bottom