How to format floating-point number to maximum fixed width in Python

I write an input file to a program with roots in the 60s and read data from fixed-width data fields in text files. Format:

  • field width 8 characters
  • floating point numbers must contain '.'or be written in exponential format, for example.'1.23e8'

The closest I got

print "{0:8.3g}".format(number)

which gives '1.23e+06's 1234567and ' 1234's 1234.

I would like to tweak this, however, to get

  • '1234567.'with 1234567(i.e. no exponential required)
  • ' 1234.'with 1234(i.e. ends with a dot, so it is not interpreted as an integer),
  • '1.235e+7's 12345678(i.e. using only one digit for the exponent),
  • '-1.23e+7' -1234567 (.. 8- ).

( ) Fortran, , , , , - ?

+4
3

, , . , , - , .

(, "+" "e", , . , , .)

, , , . doctest, unittest .

, .

+1

- , , , , - .

import unittest


class TestStringMethods(unittest.TestCase):

    def test_all(self):
        test = (
            ("1234567.", 1234567),
            ("-123456.", -123456),
            ("1.23e+13", 12345678901234),
            ("123.4567", 123.4567),
            ("123.4568", 123.45678),
            ("1.234568", 1.2345678),
            ("0.123457", 0.12345678),
            ("   1234.", 1234),
            ("1.235e+7", 12345678),
            ("-1.23e+6", -1234567),
        )

        max_char = 8
        max_number = int("9" * (max_char - 1))  # 9,999,999
        min_number = -int("9" * (max_char - 2))  # -999,999
        for expected, given in test:
            # for small numbers
            # if -999,999 < given < 9,999,999:
            if min_number < given < max_number:

                # output = f"{given:7}"
                output = f"{given:{max_char - 1}}"

                # converting ints to floats without adding zero
                if '.' not in output:
                    output += '.'

                # floats longer than 8 will need rounding to fit max length
                elif len(output) > max_char:
                    # output = str(round(given, 7 - str(given).index(".")))
                    output = str(round(given, max_char - 1 - str(given).index(".")))

            else:
                # for exponents
                # added a loop for super large numbers or negative as "-" is another char
                # Added max(max_char, 5) to account for max length of less than 5, was having too much fun
                for n in range(max(max_char, 5) - 5, 0, -1):
                    fill = f".{n}e"
                    output = f"{given:{fill}}".replace('+0', '+')
                    # if all good stop looping
                    if len(output) == max_char:
                        break
                else:
                    raise ValueError(f"Number is too large to fit in {max_char} characters", given)

            self.assertEqual(len(output), max_char, msg=output)
            self.assertEqual(output, expected, msg=given)


if __name__ == '__main__':
    unittest.main()
+1

@Harvey251, , .

:

# save the code at the end as formatfloat.py and then
import formatfloat

# do this first
width = 8
ff8 = formatfloat.FormatFloat(width)

# now use ff8 whenever you need
print(ff8(12345678901234))

. formatfloat.py , FlotFormat. , init FormatFlot.

import unittest

class FormatFloat:
    def __init__(self, width = 8):
        self.width = width
        self.maxnum = int('9'*(width - 1))  # 9999999
        self.minnum = -int('9'*(width - 2)) # -999999

    def __call__(self, x):

        # for small numbers
        # if -999,999 < given < 9,999,999:
        if x > self.minnum and x < self.maxnum:

            # o = f'{x:7}'
            o = f'{x:{self.width - 1}}'

            # converting int to float without adding zero
            if '.' not in o:
                o += '.'

            # float longer than 8 will need rounding to fit width
            elif len(o) > self.width:
                # output = str(round(x, 7 - str(x).index(".")))
                o = str(round(x, self.width-1 - str(x).index('.')))

        else:

            # for exponents
            # added a loop for super large numbers or negative as "-" is another char
            # Added max(max_char, 5) to account for max length of less 
            #     than 5, was having too much fun
            # TODO can i come up with a threshold value for these up front, 
            #     so that i dont have to do this calc for every value??
            for n in range(max(self.width, 5) - 5, 0, -1):
                fill = f'.{n}e'
                o = f'{x:{fill}}'.replace('+0', '+')

                # if all good stop looping
                if len(o) == self.width:
                    break
            else:
                raise ValueError(f"Number is too large to fit in {self.width} characters", x)
        return o


class TestFormatFloat(unittest.TestCase):
    def test_all(self):
        test = ( 
            ("1234567.", 1234567), 
            ("-123456.", -123456), 
            ("1.23e+13", 12345678901234), 
            ("123.4567", 123.4567), 
            ("123.4568", 123.45678), 
            ("1.234568", 1.2345678), 
            ("0.123457", 0.12345678), 
            ("   1234.", 1234), 
            ("1.235e+7", 12345678), 
            ("-1.23e+6", -1234567),
            )

        width = 8
        ff8 = FormatFloat(width)

        for expected, given in test:
            output = ff8(given)
            self.assertEqual(len(output), width, msg=output)
            self.assertEqual(output, expected, msg=given)

if __name__ == '__main__':
    unittest.main()
0
source

Source: https://habr.com/ru/post/1535936/


All Articles