Author: spadkins
Date: Tue Nov 28 08:07:50 2006
New Revision: 8309

Modified:
   p5ee/trunk/App-Widget/CHANGES
   p5ee/trunk/App-Widget/lib/App/Widget.pm

Log:
App::Widget->format()

Modified: p5ee/trunk/App-Widget/CHANGES
==============================================================================
--- p5ee/trunk/App-Widget/CHANGES       (original)
+++ p5ee/trunk/App-Widget/CHANGES       Tue Nov 28 08:07:50 2006
@@ -2,6 +2,9 @@
 # CHANGE LOG
 #########################################
 
+VERSION 0.965
+ x App::Widget->format() method
+
 VERSION 0.964
  x DataTable2, ImageButton: look for app-button in cgi-bin/App/app-button, not 
cgi-bin/app-button
  x TemplateEngine: do the {xyz} subs before the [%xyz%] subs. strict disallows 
{xyz} syntax

Modified: p5ee/trunk/App-Widget/lib/App/Widget.pm
==============================================================================
--- p5ee/trunk/App-Widget/lib/App/Widget.pm     (original)
+++ p5ee/trunk/App-Widget/lib/App/Widget.pm     Tue Nov 28 08:07:50 2006
@@ -528,7 +528,64 @@
             $formatted_value *= $format_options->{scale_factor};
         }
         if ($format_options->{format}) {
-            $formatted_value = sprintf($format_options->{format}, 
$formatted_value);
+            my $format = $format_options->{format};
+            if ($format =~ /%-?[0-9\.]*[sdf]/) {  # just a sprintf() type 
format
+                $formatted_value = sprintf($format, $formatted_value);
+            }
+            # This {format} style supports the following:
+            #     #.#%          percentage. multiply by 100, 1 digit precision.
+            #     +#.##%        percentage change. multiply by 100, 2 digit 
precision. add + sign at front if positive.
+            #     #,###         integer. add commas as necessary.
+            #     #,###.#       float. 1 digit precision. add commas as 
necessary.
+            #     $#,###.##     currency. 2 digits precision. add commas as 
necessary.
+            #     $+#,###.##    currency. 2 digits precision. add commas as 
necessary. add + sign at front if positive.
+            #     ($#,###.##)   currency. 2 digits precision. add commas as 
necessary. negatives in parentheses.
+            #     (#)           integer. negatives in parentheses.
+            #     (#.###)       float. 3 digits precision. negatives in 
parentheses.
+            elsif ($format =~ /^(\(?)(\$?)(\+?)([#,]*)\.?(#*)(%?)[\)]*$/) {
+                my $paren = $1;
+                my $curr  = $2;
+                my $sign  = $3;
+                my $int   = $4;
+                my $frac  = $5;
+                my $pct   = $6;
+                my $negated = ($paren && $value < 0);
+                if ($negated) {
+                    $formatted_value = -$formatted_value;
+                }
+                $formatted_value *= 100 if ($pct);  # an implied scale_factor 
of 100
+                my $precision = length($frac);
+                if ($precision > 0) {
+                    $formatted_value = sprintf("%.${precision}f", 
$formatted_value);
+                    if ($int =~ /,/) {
+                        $formatted_value =~ 
s/([0-9])([0-9][0-9][0-9])\./$1,$2./;
+                        while ($formatted_value =~ 
s/([0-9])([0-9][0-9][0-9]),/$1,$2,/) {
+                            # keep doing it until it's not found
+                        }
+                    }
+                }
+                else {
+                    $formatted_value = int($formatted_value);
+                    if ($int =~ /,/) {
+                        $formatted_value =~ s/([0-9])([0-9][0-9][0-9])$/$1,$2/;
+                        while ($formatted_value =~ 
s/([0-9])([0-9][0-9][0-9]),/$1,$2,/) {
+                            # keep doing it until it's not found
+                        }
+                    }
+                }
+                if ($curr) {
+                    $formatted_value = '$' . $formatted_value;
+                }
+                if ($sign && $value > 0) {
+                    $formatted_value = "+" . $formatted_value;
+                }
+                if ($pct) {
+                    $formatted_value = $formatted_value . "%";
+                }
+                if ($paren && $negated) {
+                    $formatted_value = "(" . $formatted_value . ")";
+                }
+            }
         }
         elsif ($format_options->{date_format}) {
             if (defined $format_options->{date_suffix}) {

Reply via email to