| 1 | Index: functions.php |
|---|
| 2 | =================================================================== |
|---|
| 3 | --- functions.php (revision 11705) |
|---|
| 4 | +++ functions.php (working copy) |
|---|
| 5 | @@ -140,13 +140,55 @@ |
|---|
| 6 | // let the user override the precision only |
|---|
| 7 | $decimals = ( is_null( $decimals ) ) ? $wp_locale->number_format['decimals'] : intval( $decimals ); |
|---|
| 8 | |
|---|
| 9 | - $num = number_format( $number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] ); |
|---|
| 10 | + $num = wp_number_format( $number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] ); |
|---|
| 11 | |
|---|
| 12 | // let the user translate digits from latin to localized language |
|---|
| 13 | return apply_filters( 'number_format_i18n', $num ); |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | + * Format number according to localization |
|---|
| 18 | + * |
|---|
| 19 | + * PHP native function number_format unfortunatelly does not support |
|---|
| 20 | + * decimal point or thousand separator longer than one character, e.g. |
|---|
| 21 | + * like |
|---|
| 22 | + * This function does support multi-char decimal point and thousands |
|---|
| 23 | + * separator. |
|---|
| 24 | + * Source code copied from http://www.php.net/number_format, comment |
|---|
| 25 | + * by team at glossword dot biz. Thank you! |
|---|
| 26 | + * |
|---|
| 27 | + * @param mixed $number The number to convert based on locale. |
|---|
| 28 | + * @param int $decimals Precision of the number of decimal places. |
|---|
| 29 | + * @param string $dec_point Decimal point string |
|---|
| 30 | + * @param string $thousands_sep Thousands separator |
|---|
| 31 | + * @return string Converted number in string format. |
|---|
| 32 | + */ |
|---|
| 33 | +function wp_number_format($n, $decimals = 0, $dec_point = '.', $thousands_sep = ',') |
|---|
| 34 | +{ |
|---|
| 35 | + $b = explode('.', $n); |
|---|
| 36 | + $fract = sprintf('%0.'.$decimals.'f', '0.'.$b[1]); |
|---|
| 37 | + if ($fract[0] == '1') { |
|---|
| 38 | + $b[0] = $b[0] + ($b[0] >= 0 ? 1 : -1); |
|---|
| 39 | + $fract[0] = '0'; |
|---|
| 40 | + } |
|---|
| 41 | + $rn = ''; |
|---|
| 42 | + $l = strlen($b[0]); |
|---|
| 43 | + /* Reverse string */ |
|---|
| 44 | + for ($i = $l; $i > 3; $i -= 3) |
|---|
| 45 | + { |
|---|
| 46 | + $rn = $thousands_sep . substr($b[0], $i - 3, 3) . $rn; |
|---|
| 47 | + } |
|---|
| 48 | + /* sprintf() used to correct 0.79 to 0.790 */ |
|---|
| 49 | + /* str_replace() used to correct decimals */ |
|---|
| 50 | + /* str_repeat() used to correct decimals */ |
|---|
| 51 | + return substr($b[0], 0, $i) . $rn . ($decimals |
|---|
| 52 | + ? $dec_point.(isset($b[1]) |
|---|
| 53 | + ? str_replace('0.', '', $fract) |
|---|
| 54 | + : str_repeat(0, $decimals)) |
|---|
| 55 | + : ''); |
|---|
| 56 | +} |
|---|
| 57 | + |
|---|
| 58 | +/** |
|---|
| 59 | * Convert number of bytes largest unit bytes will fit into. |
|---|
| 60 | * |
|---|
| 61 | * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts |
|---|