1

I'm creating a composite string for memorize id and sub id like this:

1.1

1.2

1.3

in this way:

 main_id=1 #not related to sub_id
 sub_id= 1 #or more by increment
 item = str(main_id)+"."+str(sub_id))

It works well when i pass to a number into a string. Keep the zeros. Example: 1 and using number i can increment the substring without any problem. 1.1 -> 1.2 -> 1.19 -> 1.20 - not 2.0 if I use float.

The main problem come if i want to return to a numeric type like float.

There is some way to return into a numeric type (float or other one) and keep the string content without lose anything information?

10
  • I'm not sure what you mean. Do you want to turn the string '1.20' into a float that isn't 1.2? Commented Oct 19, 2015 at 11:04
  • 1
    why do you need it as a float?
    – m.wasowski
    Commented Oct 19, 2015 at 11:06
  • 1
    @RedVelvet you can't keep the zeros - 1.20 is exactly the same as 1.200
    – grc
    Commented Oct 19, 2015 at 11:08
  • 1
    You cannot do that. 1.20 is mathematically equivalent to 1.2. Commented Oct 19, 2015 at 11:08
  • 3
    Your two-part id is not a single number, so don't try to treat it like one. Either store it in your database as two numeric fields or as a single string. It's easy enough to split the string into two numbers when you need to.
    – PM 2Ring
    Commented Oct 19, 2015 at 11:15

3 Answers 3

0

My guess is that you need to represent versions, in this case you should take a look at distutils.version module:

Type:        module
String form: <module 'distutils.version' from 
                '/usr/lib/python2.7/distutils/version.pyc'>
File:        /usr/lib/python2.7/distutils/version.py
Docstring:
Provides classes to represent module version numbers (one class for
each style of version numbering).  There are currently two such classes
implemented: StrictVersion and LooseVersion.

Every version number class implements the following interface:
  * the 'parse' method takes a string and parses it to some internal
    representation; if the string is an invalid version number,
    'parse' raises a ValueError exception
  * the class constructor takes an optional string argument which,
    if supplied, is passed to 'parse'
  * __str__ reconstructs the string that was passed to 'parse' (or
    an equivalent string -- ie. one that will generate an equivalent
    version number instance)
  * __repr__ generates Python code to recreate the version number instance
  * __cmp__ compares the current instance with either another instance
    of the same class or a string (which will be parsed to an instance
    of the same class, thus must follow the same rules)
1
  • This would make sense, but the question asks how to store these values as numbers, not strings, while retaining trailing zeros. Commented Oct 19, 2015 at 11:13
0

Try another way to store it

Ex. for '1.20', you can store it as float('1.02'), '1.200' as float('1.002')

Just reverse the sub_id because sub_id don't start with zero.

0

Try to use decimal module from python, e.g. :

from decimal import Decimal

[In]: Decimal("1.1")
[Out]: Decimal('1.1')

[In]: Decimal("1.10")
[Out]: Decimal('1.10')

[In]: Decimal("1.20")
[Out]: Decimal('1.20')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.