A function to get the right money & number formats:
<?php
function displayLocales($number, $isMoney, $lg='en_US.utf8') {
$ret = setLocale(LC_ALL, $lg);
setLocale(LC_TIME, 'Europe/Paris');
if ($ret===FALSE) {
echo "Language '$lg' is not supported by this system.\n";
return;
}
$LocaleConfig = localeConv();
forEach($LocaleConfig as $key => $val) $$key = $val;
// Sign specifications:
if ($number>0) {
$sign = $positive_sign;
$sign_posn = $p_sign_posn;
$sep_by_space = $p_sep_by_space;
$cs_precedes = $p_cs_precedes;
} else {
$sign = $negative_sign;
$sign_posn = $n_sign_posn;
$sep_by_space = $n_sep_by_space;
$cs_precedes = $n_cs_precedes;
}
// Number format:
$n = number_format(abs($number), $frac_digits,
$decimal_point, $thousands_sep);
$n = str_replace(' ', ' ', $n);
switch($sign_posn) {
case 0: $n = "($n)"; break;
case 1: $n = "$sign$n"; break;
case 2: $n = "$n$sign"; break;
case 3: $n = "$sign$n"; break;
case 4: $n = "$n$sign"; break;
default: $n = "$n [error sign_posn=$sign_posn !]";
}
// Currency format:
$m = number_format(abs($number), $frac_digits,
$mon_decimal_point, $mon_thousands_sep);
if ($sep_by_space) $space = ' '; else $space = '';
if ($cs_precedes) $m = "$currency_symbol$space$m";
else $m = "$m$space$currency_symbol";
$m = str_replace(' ', ' ', $m);
switch($sign_posn) {
case 0: $m = "($m)"; break;
case 1: $m = "$sign$m"; break;
case 2: $m = "$m$sign"; break;
case 3: $m = "$sign$m"; break;
case 4: $m = "$m$sign"; break;
default: $m = "$m [error sign_posn=$sign_posn !]";
}
if ($isMoney) return $m; else return $n;
}
$number = -12345.12345;
echo '<div>'.displayLocales($number, FALSE)."</div>\n";
echo '<div>'.displayLocales($number, TRUE)."</div>\n";
?>
localeconv
(PHP 4 >= 4.0.5, PHP 5)
localeconv — Sayısal biçemleme bilgisini döndürür
Açıklama
Sayı ve para biçemleme bilgisini içeren bir ilişkisel dizi döndürür.
Dönen Değerler
localeconv() işlevi setlocale() tarafından ayarlanan geçerli yerele özgü verinin tamamını ilişkisel bir dizi içinde döndürür. İlişkisel dizi şu alanları içerir:
| Dizi elemanı | Açıklama |
|---|---|
| decimal_point | Ondalık ayracı |
| thousands_sep | Sayı için binlik ayracı |
| grouping | Sayısal gruplama içeren dizi |
| int_curr_symbol | Uluslararası para simgesi (TRL gibi) |
| currency_symbol | Ulusal para simgesi (TL gibi) |
| mon_decimal_point | Kuruş ayracı |
| mon_thousands_sep | Para için binlik ayracı |
| mon_grouping | Parasal gruplama içeren dizi |
| positive_sign | Pozitif değerler için işaret |
| negative_sign | Negatif değerler için işaret |
| int_frac_digits | Uluslararası kuruş hane sayısı |
| frac_digits | Ulusal kuruş hane sayısı |
| p_cs_precedes | Pozitif işareti para simgesinin önünde ise TRUE ardında ise FALSE. |
| p_sep_by_space | Pozitif işareti ile para simgesi arasında bir boşluk varsa TRUE yoksa FALSE |
| n_cs_precedes | Para simgesi bir negatif değerin önündeyse TRUE ardındaysa FALSE |
| n_sep_by_space | Para simgesi ile negatif değer arasında bir boşluk varsa TRUE yoksa FALSE |
| p_sign_posn |
|
| n_sign_posn |
|
p_sign_posn ve n_sign_posn bir biçemleme seçenekleri dizgesi içerir. Her sayı yukarıda listelenen durumlardan birini gösterir.
Gruplama alanları sayıların nasıl gruplanacağını tanımlayan diziler içerir. Örneğin, tr_TR yereli için para gruplama alanı 3 ve 3 değerlerinden oluşan bir dizi içerir. Daha yüksek indis öteki gruplamaya bırakılır. Bir dizi elemanı CHAR_MAX değerine eşitse başka gruplama yapılmaz. Bir dizi elemanı 0'a eşitse önceki elemanın kullanılması gerekir.
Örnekler
Örnek 1 - localeconv() örneği
<?php
if (false !== setlocale(LC_ALL, 'tr_TR.UTF-8')) {
$locale_info = localeconv();
print_r($locale_info);
}
?>
Yukarıdaki örneğin çıktısı:
Array
(
[decimal_point] => ,
[thousands_sep] => .
[int_curr_symbol] => TRL
[currency_symbol] => TL
[mon_decimal_point] => ,
[mon_thousands_sep] => .
[positive_sign] =>
[negative_sign] => -
[int_frac_digits] => 2
[frac_digits] => 2
[p_cs_precedes] => 0
[p_sep_by_space] => 1
[n_cs_precedes] => 0
[n_sep_by_space] => 1
[p_sign_posn] => 1
[n_sign_posn] => 1
[grouping] => Array
(
[0] => 3
[1] => 3
)
[mon_grouping] => Array
(
[0] => 3
[1] => 3
)
)
localeconv
24-Nov-2007 09:15
23-Jul-2001 02:13
The C99 standard modified slightly the definition of the international currency symbol, which is now 4 characters long instead of 3 in previous definitions. The fourth character will most often be an ASCII space, but its effective value is the locale-specific spacing character used for numeric grouping (i.e. the one refered by [sep_by_space] and [grouping])...
