Florian Hengartner: September 2009 Archives

Format prices as swiss francs (CHF) in PHP - painless!

| | Comments (0)
Formatting prices as swiss francs in PHP is actually very easy .. if you know how.
Requires: PHP 5 >= 5.3.0, PECL intl >= 1.0.0

The number formatter handles all pitfalls like rounding to "5 Rappen", fraction, apostrophe's, etc.

$formatter = new NumberFormatter('de_CH', NumberFormatter::DECIMAL );
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS,2);
$formatter->setAttribute(NumberFormatter::ROUNDING_INCREMENT,0.05);
echo $formatter->formatCurrency(123456.759,'CHF')."<br />\n";
// Output: 123'456.75
echo $formatter->formatCurrency(123456,'CHF')."<br />\n";
// Output: 123'456.00
echo $formatter->formatCurrency(123456.97,'CHF')."<br />\n";
// Output: 123'456.95
echo $formatter->formatCurrency(123456.98,'CHF')."<br />\n";
// Output: 123'457.00
echo $formatter->formatCurrency(.2,'CHF')."<br />\n";
// Output: 0.20

-----------------------------------------
German:

Preise als Schweizer Franken zu Formatieren geht in PHP sehr einfach .. wenn man weiss wie.
Die NumberFormatter Klasse kann auf 5 Rappen runden, Hochkomma setzen, etc.
Benötigt wird PHP 5 >= 5.3.0, PECL intl >= 1.0.0.
Beispiel siehe oben.





About this Archive

This page is a archive of recent entries written by Florian Hengartner in September 2009.

Florian Hengartner: December 2008 is the previous archive.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.12