WordPress.org

Make WordPress Core

Ticket #10373: functions.php.diff

File functions.php.diff, 2.4 KB (added by honza.skypala, 9 years ago)

Diff solving the issue described in the ticket.

  • functions.php

     
    140140        // let the user override the precision only
    141141        $decimals = ( is_null( $decimals ) ) ? $wp_locale->number_format['decimals'] : intval( $decimals );
    142142
    143         $num = number_format( $number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
     143        $num = wp_number_format( $number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
    144144
    145145        // let the user translate digits from latin to localized language
    146146        return apply_filters( 'number_format_i18n', $num );
    147147}
    148148
    149149/**
     150 * Format number according to localization
     151 *
     152 * PHP native function number_format unfortunatelly does not support
     153 * decimal point or thousand separator longer than one character, e.g.
     154 * like  
     155 * This function does support multi-char decimal point and thousands
     156 * separator.
     157 * Source code copied from http://www.php.net/number_format, comment
     158 * by team at glossword dot biz. Thank you!
     159 *
     160 * @param mixed $number The number to convert based on locale.
     161 * @param int $decimals Precision of the number of decimal places.
     162 * @param string $dec_point Decimal point string
     163 * @param string $thousands_sep Thousands separator
     164 * @return string Converted number in string format.
     165 */
     166function wp_number_format($n, $decimals = 0, $dec_point = '.', $thousands_sep = ',')
     167{
     168    $b = explode('.', $n);
     169    $fract = sprintf('%0.'.$decimals.'f', '0.'.$b[1]);
     170                if ($fract[0] == '1') {
     171        $b[0] = $b[0] + ($b[0] >= 0 ? 1 : -1);
     172        $fract[0] = '0';
     173    }
     174    $rn = '';
     175    $l = strlen($b[0]);
     176    /* Reverse string */
     177    for ($i = $l; $i > 3; $i -= 3)
     178    {
     179        $rn = $thousands_sep . substr($b[0], $i - 3, 3) . $rn;
     180    }
     181    /* sprintf() used to correct 0.79 to 0.790 */
     182    /* str_replace() used to correct decimals */
     183    /* str_repeat() used to correct decimals */
     184    return substr($b[0], 0, $i) . $rn . ($decimals
     185            ? $dec_point.(isset($b[1])
     186                ? str_replace('0.', '', $fract)
     187                : str_repeat(0, $decimals))
     188            : '');
     189}
     190
     191/**
    150192 * Convert number of bytes largest unit bytes will fit into.
    151193 *
    152194 * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts