| | |
| | | formatter = FormatterFactory.get_formatter(formatting) |
| | | self.formatted_value = formatter.getValue(self.value) |
| | | |
| | | def format_as_tex(self, formatting=None, glyph=None, unit=None): |
| | | def format_as_tex(self, formatting=None, glyph=None, unit=None, dollar="$"): |
| | | if formatting is None: |
| | | formatting = self.formatting |
| | | formatter = FormatterFactory.get_formatter(formatting) |
| | | if formatting.split()[0] == "prefix": |
| | | leading_space = "" |
| | | else: |
| | | leading_space = "~" |
| | | if formatting == "str" or formatting == "string": |
| | | return formatter.toFormat(self.value) |
| | | elif glyph is None and unit is None: |
| | | return ("${}$").format(formatter.toFormat(self.value)) |
| | | elif glyph is None and unit is not None: |
| | | return ("${}~\mathrm{{{}}}$").format(formatter.toFormat(self.value), unit) |
| | | elif glyph is not None and unit is None: |
| | | return ("${}={}$").format(glyph, formatter.toFormat(self.value)) |
| | | else: |
| | | return ("${}={}~\mathrm{{{}}}$").format( |
| | | glyph, formatter.toFormat(self.value), unit |
| | | return ("{}{}{}{}").format( |
| | | dollar, formatter.toFormat(self.value), leading_space, dollar |
| | | ) |
| | | |
| | | def format_without_dollar(self, formatting=None): |
| | | if formatting is None: |
| | | formatting = self.formatting |
| | | formatter = FormatterFactory.get_formatter(formatting) |
| | | return formatter.toFormat(self.value) |
| | | elif glyph is None and unit is not None: |
| | | return ("{}{}{}\mathrm{{{}}}{}{}").format( |
| | | dollar, formatter.toFormat(self.value), leading_space, unit, dollar |
| | | ) |
| | | elif glyph is not None and unit is None: |
| | | return ("{}{}={}{}{}").format( |
| | | dollar, glyph, formatter.toFormat(self.value), leading_space, dollar |
| | | ) |
| | | else: |
| | | return ("{}{}={}{}\mathrm{{{}}}{}").format( |
| | | dollar, |
| | | glyph, |
| | | formatter.toFormat(self.value), |
| | | leading_space, |
| | | unit, |
| | | dollar, |
| | | ) |
| | | |
| | | def get_formatted_value(self): |
| | | return self.formatted_value |
| | |
| | | "-15":"f", |
| | | "-18":"a", |
| | | "-21":"z", |
| | | "-24":"y" |
| | | "-24": "y", |
| | | } |
| | | try: |
| | | prefix = prefixes[str(exp)] |
| | | except KeyError: |
| | | raise Exceptions.PrefixError("Could not change exponent " + str(exp) + " into prefix form!") |
| | | raise Exceptions.PrefixError( |
| | | "Could not change exponent " + str(exp) + " into prefix form!" |
| | | ) |
| | | return prefix |
| | | |
| | | |
| | | def toFormat(self, num): |
| | | try: |
| | |
| | | (exp, man) = self.realign3(exp, man) |
| | | if exp == 0: |
| | | return ( |
| | | ("{:." + str(self.precision - 1) + "f}").format(man).replace(".", ",\!") |
| | | ("{:." + str(self.precision - 1) + "f}~").format(man).replace(".", ",\!") |
| | | ) |
| | | else: |
| | | prefix = self.exp2prefix(exp) |