- $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);
+ }
+ }