1

I wanted to create numbered bullet points.

Decided the way I wanted to do it was

Strip a float of all numbers behind the decimal, but keep the decimal.

Example: 2.0 would be 2. 3.14 would be 3.

Is there way to do it this way? If so what would it look like?

Once again, Thanks in advance.

Warbit
  • 27
  • 1
  • 5

2 Answers2

1

One way to do so would be to first use int to just obtain the integer part of the float. Then if you just want a number like 3. this is impossible without converting to string since 3. give 3.0. So perhaps convert the integer part to a string then concatenate a period of the form '3.' An example:

In [1]: num = 3.14

In [2]: myint = int(num)

In [3]: myint
Out[3]: 3

In [4]: mystr = str(myint)

In [5]: mystr
Out[5]: '3'

In [6]: mystr += '.'

In [7]: mystr
Out[7]: '3.'

or in one step:

mystr = str(int(num)) + '.'

giving the output of '3.' given the input num = 3.14:

In [12]: mystr
Out[12]: '3.'
QuantumPanda
  • 283
  • 3
  • 12
1
str(int(x)) + "."

Converting back to float would add a zero after the decimal, so it has to be left in string.

Adarsh Chavakula
  • 1,509
  • 19
  • 28
  • Thanks every. Ok Azhy said not possible the way I wanted to do it, and this was quick and easy fix to get the effect I wanted. Again thank everyone for taking the time to read and those who responded – Warbit Jul 25 '18 at 16:03