JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
FIXED format_decimal(), and format_dollars/money()
authorJason Woofenden <jason@jasonwoof.com>
Wed, 24 Jun 2009 04:38:34 +0000 (00:38 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Wed, 24 Jun 2009 04:38:34 +0000 (00:38 -0400)
encode.php
format.php

index cd1e0b4..63ff5ce 100644 (file)
@@ -71,6 +71,15 @@ function enc_htmlbrtab($str) {
        return $str;
 }
 
+# Encode for output in html. Spaces converted to &nbsp; and \n to <br />
+#
+# Example: <option value="12">~foo.htmlbrnbsp~</option>
+function enc_htmlbrnbsp($str) {
+       $str = enc_htmlbr($str);
+       $str = str_replace(' ', '&nbsp;', $str);
+       return $str;
+}
+
 # Encode for output in html. Spaces converted to &nbsp;
 #
 # Example: <option value="12">~foo.htmlnbsp~</option>
index f931cdd..12615cc 100644 (file)
@@ -47,7 +47,10 @@ function format_int($str) {
 
 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) {
@@ -205,35 +208,83 @@ 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);
 }