From 32d3f2f0b9ba799d732255599e1731074a9e6e57 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Sun, 25 Nov 2018 11:09:49 +0000
Subject: [PATCH] Fixes in sci and prefix formatting and unit tests for these two classes added.

---
 aoi_gen/Variable.py |   56 ++++++++++++++++++++++++++++++--------------------------
 1 files changed, 30 insertions(+), 26 deletions(-)

diff --git a/aoi_gen/Variable.py b/aoi_gen/Variable.py
index 29009b7..745bb27 100644
--- a/aoi_gen/Variable.py
+++ b/aoi_gen/Variable.py
@@ -108,10 +108,11 @@
         pass
 
     def toFormat(self, string):
-        return string.replace("\"", "")
+        return string.replace('"', "")
 
     def getValue(self, string):
-        return "\"{}\"".format(str(string.replace("\"", "")))
+        return '"{}"'.format(str(string.replace('"', "")))
+
 
 class DecFloatFormatter(FormatterFactory):
     def __init__(self, formatparameters):
@@ -127,30 +128,27 @@
         except TypeError:
             raise ValueError
 
-        num=float(("{:."+str(self.precision-1)+"e}").format(num))
-        places=self.fexp(num)
+        num = float(("{:." + str(self.precision - 1) + "e}").format(num))
+        places = self.fexp(num)
 
-        decimal_places=self.precision-places-1
-        if(decimal_places<0):
-            decimal_places=0
-        format_str="{:"+str(places)+"."+str(decimal_places)+"f}"
+        decimal_places = self.precision - places - 1
+        if decimal_places < 0:
+            decimal_places = 0
+        format_str = "{:" + str(places) + "." + str(decimal_places) + "f}"
 
         return format_str.format(num).replace(".", ",\!")
 
-
     def getValue(self, num):
 
+        num = float(("{:." + str(self.precision - 1) + "e}").format(num))
+        places = self.fexp(num)
 
-        num=float(("{:."+str(self.precision-1)+"e}").format(num))
-        places=self.fexp(num)
-
-        decimal_places=self.precision-places-1
-        if(decimal_places<0):
-            decimal_places=0
-        format_str="{:"+str(places)+"."+str(decimal_places)+"f}"
-        val=format_str.format(num)
+        decimal_places = self.precision - places - 1
+        if decimal_places < 0:
+            decimal_places = 0
+        format_str = "{:" + str(places) + "." + str(decimal_places) + "f}"
+        val = format_str.format(num)
         return float(val)
-
 
 
 class SciFloatFormatter(FormatterFactory):
@@ -206,17 +204,19 @@
             raise ValueError
         except TypeError:
             raise ValueError
-
+        # Clip precision firstly
+        num = float(("{:." + str(self.precision - 1) + "e}").format(num))
         exp = self.fexp(num)
         man = self.fman(num)
         (exp, man) = self.realign3(exp, man)
+        decimal_places = self.precision - self.fexp(man) - 1
+        if decimal_places < 0:
+            decimal_places = 0
         if exp == 0:
-            return (
-                ("{:." + str(self.precision - 1) + "f}").format(man).replace(".", ",\!")
-            )
+            return ("{:." + str(decimal_places) + "f}").format(man).replace(".", ",\!")
         else:
             return (
-                ("{:." + str(self.precision - 1) + "f} \cdot 10^{{{}}}")
+                ("{:." + str(decimal_places) + "f} \cdot 10^{{{}}}")
                 .format(man, int(exp))
                 .replace(".", ",\!")
             )
@@ -268,18 +268,22 @@
             raise ValueError
         except TypeError:
             raise ValueError
-
+        # Clip precision firstly
+        num = float(("{:." + str(self.precision - 1) + "e}").format(num))
         exp = self.fexp(num)
         man = self.fman(num)
         (exp, man) = self.realign3(exp, man)
+        decimal_places = self.precision - self.fexp(man) - 1
+        if decimal_places < 0:
+            decimal_places = 0
         if exp == 0:
             return (
-                ("{:." + str(self.precision - 1) + "f}\,").format(man).replace(".", ",\!")
+                ("{:." + str(decimal_places) + "f}\,").format(man).replace(".", ",\!")
             )
         else:
             prefix = self.exp2prefix(exp)
             return (
-                ("{:." + str(self.precision - 1) + "f}\,\mathrm{{{}}}")
+                ("{:." + str(decimal_places) + "f}\,\mathrm{{{}}}")
                 .format(man, prefix)
                 .replace(".", ",\!")
             )

--
Gitblit v1.8.0