2

I'm taking an introductory Python course and we have an assignment that asks us to simulate the way the floating point numbers are stored.

Basically, we have to demonstrate 5 digits of Mantissa. For example, you input 123 and it must come out as 12300. If you input 12345678 it must come out as 12345. How can this be done? Is there a function for that or is it just a formmating issue? Keep in mind, this is an introductory course and we're not allowed to use any external libraries.

BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
user2961792
  • 49
  • 1
  • 4
  • I feel like your examples are wrong, or confused. – jgritty Nov 06 '13 at 18:32
  • Looks like you want a number that is always five digits and when you do not have five digits you need a zero fill. – SamA Nov 06 '13 at 18:32
  • How do I do that? And not just that, I have to remove unnecessary digits if there are more than 5. – user2961792 Nov 06 '13 at 18:34
  • Maybe cast it into a string? – jgritty Nov 06 '13 at 18:35
  • Break your problem down. First, you have to determine if your input number is more or less than five digits, and have different functionality based on which case. – NWard Nov 06 '13 at 18:35
  • I was able to figure out the length by using len(str()) function. So, there is a way for me to determine whether there are more or less than 5 characters. I just have no idea what to do next. – user2961792 Nov 06 '13 at 18:37
  • You do, though. If you have less than five characters, you need to zero-fill until you have five. That's one task. The other is if you have more than five, you need to truncate the end of the number until you have five. You have a clear idea of what you need to accomplish - consult the Python documentation on manipulating strings to learn how to implement your ideas in code: http://docs.python.org/2/library/string.html – NWard Nov 06 '13 at 18:40
  • Do you want the result to be a string or an integer ? – eyquem Nov 06 '13 at 19:15
  • “If you input `12345678` it must come out as `12345`” Your course is postponing rounding-to-the-nearest until lesson two, then? – Pascal Cuoq Nov 06 '13 at 20:21

2 Answers2

2

You can use Python's string formatting functionality:

'{:<05d}'.format(number)[:5]

Some examples:

>>> '{:<05d}'.format(123)[:5]
'12300'
>>> '{:<05d}'.format(12345678)[:5]
'12345'

Some clarification:

  • < says: align the number on the left.
  • 0 says: use 0 as padding character.
  • 5 says: set the width to at least five.
  • d says: it's a decimal.

As the number can be longer than five digits we take only the first five characters by doing [:5]. This is called slice notation and it allows you to take a slice of a string.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • This is probably the best way, you can make a function out of it that takes a number and returns the value. – jgranger Nov 06 '13 at 18:55
1

So, let's try taking the problem, and writing the rough framework of what we have to do. Notice that whenever I run into a problem that isn't immediately obvious, I push it into its own function, which I can deal with later:

string = str(number)
if len(string) > 5:
    string = truncate_string(string)
elif len(string) < 5:
    string = pad_string_with_zeros(string)

So, let's try implementing truncate_string and pad_string_with_zeros.


A string is basically a list of characters. How would we get the first 5 elements of a list? Through list slicing. Therefore, we can implement truncate_string like so:

def truncate_string(string):
    return string[:5]

How do we pad strings with zeros? Well, if you consult the Python documentation, you could use the ljust function:

>>> '11'.ljust(5, '0')
'11000'
>>> '12345678'.ljust(5, '0')
'12345'

Therefore, our function could be:

def pad_string_with_zeros(string):
    return string.ljust(5, '0')

Alternatively, we could have used a while loop:

def pad_string_with_zeros(string):
    while len(string) < 5:
        string += '0'

...although that'll be more inefficient.


If we move all our functions into the main code, then we might get something like this:

string = str(number)
if len(string) > 5:
    string = string[:5]
elif len(string) < 5:
    string = string.ljust(5, '0')

As it turns out, list slicing will not throw errors, even if you pass in too small or too large strings. For example, '12'[:5] will result in the string '12'.

Therefore, we don't need to even check for the size of the string, and can compact it down to the following lines:

string = str(number)
string = string[:5].ljust(5, '0')
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224