...
/Solution: Formatter and Hash Generation with Updated Functions
Solution: Formatter and Hash Generation with Updated Functions
A detailed review of the solutions to the previous challenge involving generating currency formats and hashes while avoiding the use of deprecated functions.
We'll cover the following...
Solution to Task 1
This challenge asked you to create a PHP script that demonstrates the use of the NumberFormatter
class to format a monetary amount in different locales. You were provided with a code snippet that set the monetary locale to Japan, Germany, France, and the United Kingdom and formatted a given monetary amount accordingly.
<?php$amt = 4395794.89;// set monetary locale to Japan$fmt = new NumberFormatter( 'ja_JP', NumberFormatter::CURRENCY );echo "Natl: " . $fmt->formatCurrency($amt, 'JPY') . "\n";$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, 'JPY');echo "Intl: " . $fmt->format($amt) . "\n";// set monetary locale to Germany$fmt = new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY );echo "Natl: " . $fmt->formatCurrency($amt, 'EUR') . "\n";$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, 'EUR');echo "Intl: " . $fmt->format($amt) . "\n";// set monetary locale to France$fmt = new NumberFormatter( 'fr_FR', NumberFormatter::CURRENCY );echo "Natl: " . $fmt->formatCurrency($amt, 'EUR') . "\n";$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, 'EUR');echo "Intl: " . $fmt->format($amt) . "\n";// set monetary locale to United Kingdom$fmt = new NumberFormatter( 'en_GB', NumberFormatter::CURRENCY );echo "Natl: " . $fmt->formatCurrency($amt, 'GBP') . "\n";$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, 'GBP');echo "Intl: " . $fmt->format($amt) . "\n";?>
Let’s get into the code.
Generating code for Japanese currency JPY:
Line 5: Create a variable
$fmt
and set the monetary locale to Japanese currency JPY.Line 6: Display the currency in a national format using ...