No Truncate Function in PHP - Parameters?

I have an algorithm that performs the following calculations:

  • ((0.50 * 0) + 7) / 10 = 0.70
  • ((0.70 * 10) + 9) / 20 = 0.80
  • ((0.80 * 20) + 7) / 30 = 0.7666666667 → I want this value to be truncated to 0.76

So that he can feed the rest of the calculations, follow these steps:

4a. (( 0.76 * 30) + 8) / 40 = 0.77

And not for filing when rounding to two decimal places like 0.77 :

4b. (( 0.77 * 30) + 8) / 40 = 0.77

**** This seems to have failed and instead rounds to 0.77: ****

PHP function sptrinf (): PHP discards decimals without rounding

PHP function number_format (): PHP: displays a number up to two decimal places

PHP function floor (): Trim floating-point numbers with PHP

  • Is there another way?
  • Is it possible to achieve what I want (truncate to two decimal places) using PHP?

Help. Many thanks.

UPDATE - SOLVED Okay, now it works thanks to dkamins and zneak. I used the floor () approach (I assume that in the past I did not do something like that). However, now the following happens:

eg.

(0.86 * 30) + 9) / 40 = 0.87 (he should), but after TRUNC it = 0.86

How does he trim 0.87 to 0.86? That doesn't make any sense. Is there a way to truncate it if there are more than two decimal places?

SOLVE:

$numDecPlace = strlen(substr(strrchr($newRel, "."), 1));
echo '<p>Test: Number of decimal places=' .$numDecPlace. '</p>';
if($numDecPlace > 2) {
    $newRel = floor($newRel * 100) / 100; // Truncate to 2dp.
    echo '<p>Test: New relationship truncated is $newRel=' .$newRel. '</p>';
}
+5
source share
5

PHP floor, :

floor(0.7666666667 * 100) / 100;
+4

, , . , .

function truncate($number, $decimals)
{
  $point_index = strrpos($number, '.'); 
  return substr($number, 0, $point_index + $decimals+ 1);
}

$number - , $decimals - , . : truncate(-38.59540719940386 , 6); -38.595407

, substr !

+4

round()

  <?php
  echo round(3.4);         // 3
  echo round(3.5);         // 4
  echo round(3.6);         // 4
  echo round(3.6, 0);      // 4
  echo round(1.95583, 2);  // 1.96
  echo round(1241757, -3); // 1242000
  echo round(5.045, 2);    // 5.05
  echo round(5.055, 2);    // 5.06
  ?>
+2

+ ve -ve , :
- php floor() + ve
- php ceil() -ve

function truncate_float($number, $decimals) {
    $power = pow(10, $decimals); 
    if($number > 0){
        return floor($number * $power) / $power; 
    } else {
        return ceil($number * $power) / $power; 
    }
}

, floor() , .
.. floor()
floor(1.5) = 1, floor(-1.5) = 2

multiply by power, round, then divide by power:
- floor()
- ceil()

, http://phpfiddle.org/lite ( ):

<div>Php Truncate Function</div>
<br>
<?php
    function truncate_float($number, $places) {
        $power = pow(10, $places); 
        if($number > 0){
            return floor($number * $power) / $power; 
        } else {
            return ceil($number * $power) / $power; 
        }
    }

    // demo
    $lat = 52.4884;
    $lng = -1.88651;
    $lat_tr = truncate_float($lat, 3);
    $lng_tr = truncate_float($lng, 3);
    echo 'lat = ' . $lat . '<br>';
    echo 'lat truncated = ' . $lat_tr . '<br>';
    echo 'lat = ' . $lng . '<br>';
    echo 'lat truncated = ' . $lng_tr . '<br><br>';

    // demo of floor() on negatives
    echo 'floor (1.5) = ' . floor(1.5) . '<br>';
    echo 'floor (-1.5) = ' . floor(-1.5) . '<br>';
?>
+1

round(), Kartikey. , PHP.Net

float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )

FiSSH

0

All Articles