[Mumps2Py:] [118] added Mf_number for the $FNUMBER intrinsic function.

[ Thread Index | Date Index | More lists.mumps2py.org/discuss Archives ]


Revision: 118
Author:   pgallot
Date:     2008-02-18 20:53:00 +0000 (Mon, 18 Feb 2008)

Log Message:
-----------
added Mf_number for the $FNUMBER intrinsic function.

Modified Paths:
--------------
    trunk/mumps2py/mumpsCL.py


Modified: trunk/mumps2py/mumpsCL.py
===================================================================
--- trunk/mumps2py/mumpsCL.py	2008-02-18 20:50:10 UTC (rev 117)
+++ trunk/mumps2py/mumpsCL.py	2008-02-18 20:53:00 UTC (rev 118)
@@ -75,6 +75,44 @@
     else:
         return val + len(substr) + 1
 
+def Mf_fnumber(num, format, places = 0):
+    """Python-equivalent of the Mumps $fnumber intrinsic"""
+    num_is_positive = (num >= 0.0)
+    
+    num_str = "%.*f" % (places, abs(num))
+    if format.find(",") != -1: # insert a thousands-separating comma.
+        point = num_str.find(".")
+        integer = (point == -1) and num_str or num_str[:point]
+        decimal = (point != -1) and num_str[point:] or ""
+        count = 0
+        formatted = []
+        for i in xrange(len(integer), 0, -1):
+            count += 1
+            formatted.append(integer[i - 1])
+            if count % 3 == 0 and i - 1:
+                formatted.append(",")
+        num_str = "".join(formatted[::-1]) + decimal
+
+    if (format.upper().find("P") != -1): # use parens for negative numbers
+        if num_is_positive:
+            num_str = " %s " % num_str
+        else:
+            num_str = "(%s)" % num_str
+    else:
+        force_pos_sign = (format.find("+") != -1)
+        suppress_neg_sign = (format.find("-") != -1)
+        sign_str = ""
+        if force_pos_sign and num_is_positive:
+            sign_str = "+"
+        elif not (num_is_positive or suppress_neg_sign):
+            sign_str = "-"
+        if format.upper().find("T") != -1: # output a trailing sign
+            num_str = "%s%s" % (num_str, sign_str)
+        else:
+            num_str = "%s%s" % (sign_str, num_str)
+
+    return num_str
+
 def Mf_order(var, var_index, direction = 1):
     """Python-equivalent of the Mumps $order intrinsic"""
     assert(direction == 1 or direction == -1)


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/