[LON-CAPA-cvs] cvs: loncom /homework default_homework.lcpm doc/homework homework5.html

albertel lon-capa-cvs@mail.lon-capa.org
Wed, 17 Jul 2002 18:08:39 -0000


albertel		Wed Jul 17 14:08:39 2002 EDT

  Modified files:              
    /doc/homework	homework5.html 
    /loncom/homework	default_homework.lcpm 
  Log:
  - added support for formatting numbers easily into money amounts. BUG#520
  
  
Index: doc/homework/homework5.html
diff -u doc/homework/homework5.html:1.18 doc/homework/homework5.html:1.19
--- doc/homework/homework5.html:1.18	Wed Jul 17 13:51:46 2002
+++ doc/homework/homework5.html	Wed Jul 17 14:08:38 2002
@@ -671,18 +671,24 @@
     <tr>
              <td valign="top">/DIS($x,"nn")</td>
              <td valign="top">&amp;format($x,"nn")</td>
-             <td valign="top">Display or format $x as nn where nn is nF or nE and n is an integer.</td>
+             <td valign="top">Display or format $x as nn where nn is nF or nE and n is an integer. Also supports the first character being a $, it thjen will format the result with a call to &dollarformat() described below.</td>
              <td valign="top">&nbsp;The difference is obvious.</td>
     </tr>
 
     <tr>
              <td valign="top">Not in CAPA</td>
              <td valign="top">&amp;prettyprint($x,"nn")</td>
-             <td valign="top">Display or format $x as nn where nn is nF or nE and n is an integer. In E mode it will attempt to generate a pretty x10^3 rather than a E3 following the number</td>
+             <td valign="top">Display or format $x as nn where nn is nF or nE and n is an integer. Also supports the first character being a $, it then will format the result with a a call to &dollarformat() described below. In E mode it will attempt to generate a pretty x10^3 rather than a E3 following the number</td>
              <td valign="top">&nbsp;</td>
     </tr>
 
     <tr>
+             <td valign="top">Not in CAPA</td>
+             <td valign="top">&amp;dollarformat($x</td>
+             <td valign="top">Reformats $x to have a $ (or \$ if in tex mode) and to have , grouping thousands.</td>
+             <td valign="top">&nbsp;</td>
+    </tr>
+    <tr>
              <td valign="top">roundto(x,n)</td>
              <td valign="top">&amp;roundto($x,$n)</td>
              <td valign="top">Rounds a real number to n decimal points. $x and
@@ -1153,7 +1159,7 @@
     <address><a href="mailto:albertel@marvin.lite.msu.edu">Guy Albertelli</a></address>
 <!-- Created: Thu May 17 15:05:35 EDT 2001 -->
 <!-- hhmts start -->
-Last modified: Fri Jul 12 18:59:29 EDT 2002
+Last modified: Fri Jul 12 21:28:44 EDT 2002
 <!-- hhmts end -->
   </body>
 </html>
Index: loncom/homework/default_homework.lcpm
diff -u loncom/homework/default_homework.lcpm:1.47 loncom/homework/default_homework.lcpm:1.48
--- loncom/homework/default_homework.lcpm:1.47	Fri May 31 18:19:06 2002
+++ loncom/homework/default_homework.lcpm	Wed Jul 17 14:08:39 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
 #
-# $Id: default_homework.lcpm,v 1.47 2002/05/31 22:19:06 albertel Exp $
+# $Id: default_homework.lcpm,v 1.48 2002/07/17 18:08:39 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -442,29 +442,59 @@
 
 sub format {
   my ($value,$fmt)=@_;
-  return sprintf('%.'.$fmt,$value);
+  my $dollarmode;
+  if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; } 
+  my $result=sprintf('%.'.$fmt,$value);
+  if ($dollarmode) {$result=&dollarmode($result);}
+  return $result;
 }
 
 sub prettyprint {
   my ($value,$fmt)=@_;
   my $result;
+  my $dollarmode;
+  if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; } 
   if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
   if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
     my $frac=$1;
+    if ($dollarmode) { $frac=&dollarformat($frac); }
     my $exponent=$2;
     $exponent=~s/^\+0*//;
-    if (!$exponent) { return $frac; }
-    if ($external::target eq 'web') {
-      $result=$frac.'&#215;10<sup>'.$exponent.'</sup>';
-    } elsif ($external::target eq 'tex') {
-      $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
+    if ($exponent) {
+      if ($external::target eq 'web') {
+	$result=$frac.'&#215;10<sup>'.$exponent.'</sup>';
+      } elsif ($external::target eq 'tex') {
+	$result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
+      } else {
+	$result=$value;
+      }
     } else {
-      $result=$value;
+      $result=$frac;
     }
   } else {
     $result=$value;
+    if ($dollarmode) { $result=&dollarformat($result); }
   }
   return $result;
+}
+
+sub dollarformat {
+  my ($number) = @_;
+  if ($number =~ /\./) {
+    while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*$)/) {
+      $number = $1.','.$2.$3;
+    }
+  } else {
+    while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
+      $number = $1.','.$2.$3;
+    }
+  }
+  if ($external::target eq 'tex') {
+    $number='\$'.$number; #' stupid emacs
+  } else {
+    $number='$'.$number; #' stupid emacs
+  }
+  return $number; 
 }
 
 sub map {