Как заставить number_format() не округлять числа

У меня есть этот номер:

$double = '21.188624';

После использования number_format($double, 2, ',', ' ') Я получил:

21,19

Но то, что я хочу, это:

21,18

Любая идея, как я могу сделать эту работу?

Спасибо.

19 ответов

Решение

number_format всегда будет делать это, ваше единственное решение состоит в том, чтобы кормить его чем-то другим:

$number = intval(($number*100))/100;

Или же:

$number = floor(($number*100))/100;

Я знаю, что это старый вопрос, но он все еще актуален:) .

Как насчет этой функции?

function numberFormatPrecision($number, $precision = 2, $separator = '.')
{
    $numberParts = explode($separator, $number);
    $response = $numberParts[0];
    if(count($numberParts)>1){
        $response .= $separator;
        $response .= substr($numberParts[1], 0, $precision);
    }
    return $response;
}

Использование:

// numbers test
numberFormatPrecision(19, 2, '.'); // expected 19 return 19
numberFormatPrecision(19.1, 2, '.'); //expected 19.1 return 19.1
numberFormatPrecision(19.123456, 2, '.'); //expected 19.12 return 19.12

// negative numbers test
numberFormatPrecision(-19, 2, '.'); // expected -19 return -19
numberFormatPrecision(-19.1, 2, '.'); //expected -19.1 return -19.1
numberFormatPrecision(-19.123456, 2, '.'); //expected -19.12 return -19.12

// precision test
numberFormatPrecision(-19.123456, 4, '.'); //expected -19.1234 return -19.1234

// separator test
numberFormatPrecision('-19,123456', 3, ','); //expected -19,123 return -19,123  -- comma separator

Функция (только точность):

function numberPrecision($number, $decimals = 0)
{
    $negation = ($number < 0) ? (-1) : 1;
    $coefficient = pow(10, $decimals);
    return $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
}

Примеры:

numberPrecision(2557.9999, 2);     // returns 2557.99
numberPrecision(2557.9999, 10);    // returns 2557.9999
numberPrecision(2557.9999, 0);     // returns 2557
numberPrecision(2557.9999, -2);    // returns 2500
numberPrecision(2557.9999, -10);   // returns 0
numberPrecision(-2557.9999, 2);    // returns -2557.99
numberPrecision(-2557.9999, 10);   // returns -2557.9999
numberPrecision(-2557.9999, 0);    // returns -2557
numberPrecision(-2557.9999, -2);   // returns -2500
numberPrecision(-2557.9999, -10);  // returns 0

Функция (полная функциональность):

function numberFormat($number, $decimals = 0, $decPoint = '.' , $thousandsSep = ',')
{
    $negation = ($number < 0) ? (-1) : 1;
    $coefficient = pow(10, $decimals);
    $number = $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
    return number_format($number, $decimals, $decPoint, $thousandsSep);
}

Примеры:

numberFormat(2557.9999, 2, ',', ' ');     // returns 2 557,99
numberFormat(2557.9999, 10, ',', ' ');    // returns 2 557,9999000000
numberFormat(2557.9999, 0, ',', ' ');     // returns 2 557
numberFormat(2557.9999, -2, ',', ' ');    // returns 2 500
numberFormat(2557.9999, -10, ',', ' ');   // returns 0
numberFormat(-2557.9999, 2, ',', ' ');    // returns -2 557,99
numberFormat(-2557.9999, 10, ',', ' ');   // returns -2 557,9999000000
numberFormat(-2557.9999, 0, ',', ' ');    // returns -2 557
numberFormat(-2557.9999, -2, ',', ' ');   // returns -2 500
numberFormat(-2557.9999, -10, ',', ' ');  // returns 0
floor($double*100)/100

Я использую эту функцию:

function cutNum($num, $precision = 2){
    return floor($num).substr($num-floor($num),1,$precision+1);
}

floor($num) целая часть, и $num-floor($num) является десятичным (например, 0,2345).

Так substr($num-floor($num),1,$precision+1) получает ".23" (обратите внимание, что точка взята также).

Примеры использования:

cutNum(1.2345)    //returns 1.23
cutNum(1.2345,3)  //returns 1.234
cutNum(1.2345,10) //returns 1.2345
cutNum(1.2345,0)  //returns 1. (!don't use with zero as second argument: use floor instead!)

Используйте встроенную функцию PHP bcdiv.

function numberFormat($number, $decimals = 2, $sep = ".", $k = ","){
    $number = bcdiv($number, 1, $decimals); // Truncate decimals without rounding
    return number_format($number, $decimals, $sep, $k); // Format the number
}

Смотрите этот ответ для более подробной информации.

 **Number without round**        

   $double = '21.188624';
   echo intval($double).'.'.substr(end(explode('.',$double)),0,2);

**Output** 21.18

Если вам нужно 2 фиксированных десятичных знака, вы можете попробовать это!

Решение @Dima у меня работает, но оно печатает «19,90» как «19,9», поэтому я внес некоторые изменения следующим образом:

      <?php 
function numberPrecision($number, $decimals = 0)
    {
        $negation = ($number < 0) ? (-1) : 1;
        $coefficient = 10 ** $decimals;
        $result = $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
        $arr = explode(".", $result);
        $num = $arr[0];
        if(empty($arr[1]))
            $num .= ".00";
        else if(strlen($arr[1]) == 1)
            $num .= "." . $arr[1] . "0";
        else
            $num .= ".". $arr[1];
        return $num;
    }
    echo numberPrecision(19.90,2); // 19.90

Итак, что я сделал, так это просто разбил результат на две части с помощью функции разнесения. и преобразовать результат в строку с конкатенацией!

$double = '21.188624';

$teX = explode('.', $double);

if(isset($teX[1])){
    $de = substr($teX[1], 0, 2);
    $final = $teX[0].'.'.$de;
    $final = (float) $final;
}else{
    $final = $double;   
}

финал будет 21,18

Если вам не важно, что стоит за десятичной точкой, вы можете разыграть float как int чтобы избежать округления:

$float = 2.8;
echo (int) $float; // outputs '2'
$finalCommishParts = explode('.',$commission);
$commisshSuffix = (isset($finalCommishParts[1])?substr($finalCommishParts[1],0,2):'00');
$finalCommish = $finalCommishParts[0].'.'.$commisshSuffix;

спасибо за помощь Диме!!! Моя функция

          private function number_format(float $num, int $decimals = 0, ?string $decimal_separator = ',', ?string $thousands_separator = '.'){
     /**
      * Formatea un numero como number_format sin redondear hacia arriba, trunca el resultado
     * @access private
     * @param string num - Numero de va a ser formateado
     * @param int decimals - Posiciones Decimales
     * @param string|null $decimal_separator — [opcional]
     * @param string|null $thousands_separator — [opcional]
     * @return string — Version de numero formateado.
     */

     $negation = ($num < 0) ? (-1) : 1;
     $coefficient = 10 ** $decimals;
     $number = $negation * floor((string)(abs($num) * $coefficient)) / $coefficient;
     return number_format($number, $decimals, $decimal_separator, $thousands_separator);
}

для использования

      echo $this->number_format(24996.46783, 3, ',', '.'); //24.996,467

Лучшее решение

        echo number_format(111114,2,'.','');
public function numberFormatPrecision( $number, $separator = '.', $format = 2 ){

    $response = '';
    $brokenNumber = explode( $separator, $number );
    $response = $brokenNumber[0] . $separator;
    $brokenBackNumber = str_split($brokenNumber[1]);

    if( $format < count($brokenBackNumber) ){

        for( $i = 1; $i <= $format; $i++ )
            $response .= $brokenBackNumber[$i];
    }

    return $response;
}

Более быстрый способ взрыва (построение массивов) - сделать это с помощью строковых команд:

$number = ABC.EDFG;
$precision = substr($number, strpos($number, '.'), 3); // 3 because . plus 2 precision  
$new_number = substr($number, 0, strpos($number, '.')).$precision;

В этом случае результат равен ABC.ED из-за точности 2 Если вы хотите большей точности, просто измените 3 на 4 или X, чтобы получить точность X-1

ура

Версия Javascript

      function numberFormat($number, $decimals = 0, $decPoint = '.' , $thousandsSep = ',')
{
    return number_format((Math.floor($number * 100) / 100).toFixed($decimals), $decimals, $decPoint, $thousandsSep );
}
 // https://locutus.io/php/strings/number_format/
function number_format(number, decimals, decPoint, thousandsSep) {
    if(decimals === 'undefined') decimals = 2;

    number = (number + '').replace(/[^0-9+\-Ee.]/g, '')
  const n = !isFinite(+number) ? 0 : +number
  const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
  const sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep
  const dec = (typeof decPoint === 'undefined') ? '.' : decPoint
  let s = ''
  const toFixedFix = function (n, prec) {
    if (('' + n).indexOf('e') === -1) {
      return +(Math.round(n + 'e+' + prec) + 'e-' + prec)
    } else {
      const arr = ('' + n).split('e')
      let sig = ''
      if (+arr[1] + prec > 0) {
        sig = '+'
      }
      return (+(Math.round(+arr[0] + 'e' + sig + (+arr[1] + prec)) + 'e-' + prec)).toFixed(prec)
    }
  }
  // @todo: for IE parseFloat(0.55).toFixed(0) = 0;
  s = (prec ? toFixedFix(n, prec).toString() : '' + Math.round(n)).split('.')
  if (s[0].length > 3) {
    s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep)
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || ''
    s[1] += new Array(prec - s[1].length + 1).join('0')
  }
  return s.join(dec)
}
          $number = 2.278;
    echo new_number_format($number,1);
    //result: 2.2

    function new_number_format($number,$decimal)
    {
        //explode the number with the delimiter of dot(.) and get the whole number in index 0 and the decimal in index 1
        $num = explode('.',$number);
        //if the decimal is equal to zero
        //take note that we can't split the zero value only and it will return Undefined offset if we split the zero only
        //for example: rating_format(2.0,1); the result will be 2. the zero is gone because of the Undefined offset
        //the solution of this problem is this condition below
        if($num[1] == 0)
        {
          $final_decimal = '';
          $i=0;
          //loop the decimal so that we can display depend on how many decimal that you want to display
          while($i<$decimal){
            $final_decimal .= 0;
            $i++;
          }
        }
        //if the decimal is not zero
        else
        {
          $dec = str_split($num[1]); //split the decimal and get the value using the array index
          $i=0;
          $final_decimal = '';
          //loop the decimal so that we can display depend on how many decimal that you want to display
          while($i<$decimal){
            $final_decimal .= $dec[$i];
            $i++;
            }
        }
          $new_number= $num[0].'.'.$final_decimal;//combine the result with final decimal
          return $new_number; //return the final output
    }

В случае, если у вас есть маленький float значения, которые вы можете использовать number_format функционировать таким образом.

$number = 21.23;

echo number_format($number, 2, '.', ',') ); // 21.23

Если у вас есть длинное десятичное число, то оно также отформатирует число таким образом

$number = 201541.23;

echo number_format($number, 2, '.', ',') ); // 201,541.23

Используйте эту функцию:

function number_format_unlimited_precision($number,$decimal = '.')
{
   $broken_number = explode($decimal,$number);
   return number_format($broken_number[0]).$decimal.$broken_number[1]);
}
Другие вопросы по тегам