function fp_truncate_decimals

7.x misc.inc fp_truncate_decimals($num, $places = 2)
6.x misc.inc fp_truncate_decimals($num, $places = 2)
5.x misc.inc fp_truncate_decimals($num, $places = 2)

This simple function will take a number and truncate the number of decimals to the requested places. This can be used in place of number_format(), which *rounds* numbers.

For example, number_format(1.99999, 2) gives you 2.00. But THIS function gives: fp_truncate_decimals(1.999999, 2) = 1.99

Parameters

unknown_type $places:

8 calls to fp_truncate_decimals()
fp_render_currently_advising_box in includes/theme.inc
Draws the CurrentlyAdvisingBox which appears at the top of the screen, containing the student's information like name, major, etc.
stats.module in modules/stats/stats.module
This module displays statistics and reports for FlightPath
stats_test in modules/stats/stats.module
Lets me run a test...
theme.inc in includes/theme.inc
_AdvisingScreen.php in classes/_AdvisingScreen.php

... See full list

File

includes/misc.inc, line 2163
This file contains misc functions for FlightPath

Code

function fp_truncate_decimals($num, $places = 2) {

  // does $num contain a .?  If not, add it on.
  if (!strstr("" . $num, ".")) {
    $num .= ".0";
  }

  // Break it by .
  $temp = explode(".", "" . $num);

  // Get just the decimals and trim 'em
  $decimals = trim(substr($temp [1], 0, $places));
  if (strlen($decimals) < $places) {
    // Padd with zeros on the right!
    $decimals = str_pad($decimals, $places, "0", STR_PAD_RIGHT);
  }

  $new_num = $temp [0] . "." . $decimals;

  return $new_num;
}