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.





Leave a comment

About this Entry

This page contains a single entry by Florian Hengartner published on September 18, 2009 4:34 PM.

Fix crashing Unreal Tournament 2004 (on mac osx) was the previous entry in this blog.

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

Powered by Movable Type 4.12