JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
API CHANGE: removed format_options() (unused?)
[wfpl.git] / format.php
index f931cdd..0fbc88f 100644 (file)
@@ -24,30 +24,18 @@ function format_caption($str) {
        return str_replace('Email', 'E-mail', $str);
 }
 
-# This function makes sure that $str is in the list of options, and returns "" otherwise
-function format_options($str, $name) {
-       if(!isset($GLOBALS[$name . '_options'])) {
-               die("Couldn't find options for \"$name\". Be sure to call pulldown().");
-       }
-
-       foreach($GLOBALS[$name . '_options']['options'] as $keyval) {
-               list($key, $value) = $keyval;
-               if($str == $key) {
-                       return $str;
-               }
-       }
-
-       return $str;
-}
-
 function format_int($str) {
        $str = ereg_replace('[^0-9]', '', $str);
        return ereg_replace('^0*([0-9])', '\1', $str);
 }
 
+# format the digits after the decimal point
 function format_decimals($str) {
        $str = ereg_replace('[^0-9]', '', $str);
-       return ereg_replace('^([0-9])0*', '\1', $str);
+       if(strlen($str)) {
+               $str = substr($str, 0, 1) . ereg_replace('0*$', '', substr($str, 1));
+       }
+       return $str;
 }
 
 function _format_positive_decimal($str) {
@@ -96,6 +84,7 @@ function format_int_0($str) {
        return $str;
 }
 
+# USA zip codes
 function format_zip($str) {
        $str = ereg_replace('[^0-9]', '', $str);
        if(strlen($str) > 5) {
@@ -143,7 +132,6 @@ function format_image_w_h($str) {
 function format_image_w_h_thumb_w_h($str) {
        $fields = explode(' ', $str);
        if(count($fields) != 6) {
-               die('count: ' . count($fields));
                return '';
        }
 
@@ -205,41 +193,90 @@ function format_url($str) {
        return $str;
 }
 
+# pass a string containing only numeric digits.
+# returns a string containing only numeric digits which is 1 greater.
+function strplusone($str) {
+       $ret = '';
+       $a = str_split($str);
+       $carry = 1;
+       while($a) {
+               $digit = array_pop($a);
+               $digit += $carry;
+               $carry = 0;
+               if($digit == 10) {
+                       $carry = 1;
+                       $digit = 0;
+               }
+               $ret = "$digit$ret";
+       }
+       if($carry) {
+               $ret = "$carry$ret";
+       }
+
+       return $ret;
+}
+
+# rounds properly to the nearest penny (or dollar if you pass false for the 2nd
+# parameter) and prints like so: $12,456.79 or $12,457
 function format_money($str, $display_cents = true) {
-       $str = ereg_replace('[^0-9.]', '', $str);
-       if($display_cents) {
-               $int = (int)($str * 100);
-               $cents = $int % 100;
-               $cents = sprintf('.%02d', $cents);
-               $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
+       $str = format_decimal($str);
+       if($str == '') {
+               $str = '0';
+       }
+       if(strpos($str, '.')) {
+               list($int, $decimals) = explode('.', $str);
+               if(strlen($decimals) == 1) {
+                       $decimals .= '0';
+               }
+               if($display_cents) {
+                       if(strlen($decimals) > 2) {
+                               # round up to the nearest penny
+                               if(substr($decimals, 2, 1) >= 5) {
+                                       $decimals = strplusone(substr($decimals, 0, 2));
+                                       if($decimals == '100') {
+                                               $decimals = '00';
+                                               $int = strplusone($int);
+                                       }
+                               } else {
+                                       $decimals = substr($decimals, 0, 2);
+                               }
+                       }
+                       $cents = ".$decimals";
+               } else {
+                       if(substr($decimals, 0, 1) >= 5) {
+                               $int = strplusone($int);
+                       }
+               }
        } else {
-               $cents = '';
-               $int = round($str);
+               $int = $str;
+               if($display_cents) {
+                       $cents = '.00';
+               }
        }
-       $chars = (string)$int;
+       $chars = str_split($int);
        $output = '';
        $comma = 4;
-       $index = strlen($chars);
-       while($index) {
-               --$index;
+       while($chars) {
                --$comma;
                if($comma == 0) {
                        $comma = 3;
                        $output = ',' . $output;
                }
-               $char = substr($chars, $index, 1);
+               $char = array_pop($chars);
                $output = $char . $output;
        }
        $output = '$' . $output . $cents;
        return $output;
 }
 
+
 function format_dollars($str) {
        return format_money($str, false);
 }
 
 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
 function format_mdy_to_ymd($str) {
+       if($str == '') return '';
        require_once('code/wfpl/time.php');
        return mdy_to_ymd(format_oneline($str));
 }