title: No Truncate function in PHP - Options?
body: <p>I have an algorithm that performs the following calculations:</p>
  
  <ol>
  <li>( ( 0.50 * 0 ) + 7 ) / 10 = 0.70</li>
  <li>( ( 0.70 * 10 ) + 9 ) / 20 = 0.80</li>
  <li>( ( 0.80 * 20 ) + 7 ) / 30 = 0.7666666667 -> I want this value to truncate to <strong>0.76</strong></li>
  </ol>
  
  <p>So that it may feed into the rest of the calculation as:</p>
  
  <p>4a. ( ( <strong>0.76</strong> * 30 ) + 8 ) / 40 = 0.77</p>
  
  <p>And <strong>not</strong> to feed in when rounded up two decimal places as <strong>0.77</strong>:</p>
  
  <p>4b. ( ( <strong>0.77</strong> * 30 ) + 8 ) / 40 = 0.77</p>
  
  <p>****The following seem to have failed and instead force a round up to 0.77:****</p>
  
  <p>The PHP sptrinf() function: <a href="https://stackoverflow.com/questions/9079158/php-dropping-decimals-without-rounding-up">PHP dropping decimals without rounding up</a> </p>
  
  <p>The PHP number_format() function: <a href="https://stackoverflow.com/questions/4483540/php-show-a-number-to-2-decimal-places">PHP: show a number to 2 decimal places</a></p>
  
  <p>The PHP floor() function: <a href="https://stackoverflow.com/questions/4668628/truncate-float-numbers-with-php">Truncate float numbers with PHP</a></p>
  
  <ul>
  <li>Is there another way? </li>
  <li>Is it at all possible to achieve what I want (truncate to two decimal places) with PHP?</li>
  </ul>
  
  <p>Please assist. Many thanks.</p>
  
  <p><strong>UPDATE - SOLVED</strong>
  Ok, it is working now thanks to dkamins and zneak. I used the floor() approach (I assume I wasn't doing something right in the past). <em>However, now the following happens:</em></p>
  
  <p>e.g.</p>
  
  <p>(0.86 * 30) + 9 ) / 40 = 0.87 (it should), yet after TRUNC it = 0.86</p>
  
  <p>How is it truncating 0.87 to 0.86? It makes no sense. Is there a way to get it to truncate only if there are more than 2 decimal places?</p>
  
  <p><em>SOLVED:</em></p>
  
  <pre><code>$numDecPlace = strlen(substr(strrchr($newRel, "."), 1));
  echo '&lt;p&gt;Test: Number of decimal places=' .$numDecPlace. '&lt;/p&gt;';
  if($numDecPlace &gt; 2) {
      $newRel = floor($newRel * 100) / 100; // Truncate to 2dp.
      echo '&lt;p&gt;Test: New relationship truncated is $newRel=' .$newRel. '&lt;/p&gt;';
  }
  </code></pre>