From 7a7284ccc3caba3bf7cf69e8fb806e4478bee0b1 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Wed, 24 Jun 2009 00:38:34 -0400 Subject: [PATCH] FIXED format_decimal(), and format_dollars/money() --- encode.php | 9 +++++++ format.php | 79 +++++++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 14 deletions(-) diff --git a/encode.php b/encode.php index cd1e0b4..63ff5ce 100644 --- a/encode.php +++ b/encode.php @@ -71,6 +71,15 @@ function enc_htmlbrtab($str) { return $str; } +# Encode for output in html. Spaces converted to   and \n to
+# +# Example: +function enc_htmlbrnbsp($str) { + $str = enc_htmlbr($str); + $str = str_replace(' ', ' ', $str); + return $str; +} + # Encode for output in html. Spaces converted to   # # Example: diff --git a/format.php b/format.php index f931cdd..12615cc 100644 --- a/format.php +++ b/format.php @@ -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); } -- 1.7.10.4