0

I thing split() function do not work correctly if I want split number as string which one I converted from float.

Main question is how to fix my code.?

To show you my problem I wrote short script in python. You can copy and run.

#!/usr/bin/env python3
# coding=utf-8

def f_test(number):
    print(f"I will split {number}")
    if isinstance(number, float):
        print('This number is float')
        number = str(number)
        print(f'I changed type to str: {type(number)}')
    else:
        print('This number is string')
    l1 = number.split('.')
    print(l1)
    print()


f_test(2.12)
f_test('2.12')

f_test(0.1)
f_test('0.1')

f_test(0.000000001)
f_test('0.000000001')

f_test(0.000107)
f_test('0.000107')

f_test(0.0000107)
f_test('0.0000107')

Because i do not know the type of this number, I have to check is that float or string. If is float I convert it to str.

In my opinion is something wrong with conversion float to string, because after conversion python show type as string, but print show the following form significand *(base)^exponent for float

For test i used numbers as float and str. That what i got you can see bellow.

Strange behavior for 0.000000001 and 0.0000107.

Generally split doesn't work correctly for all numbers as float converted to string In situation when after conversion print show float representation like this 1.07e-5

I will split 2.12
This number is float
I changed type to str: <class 'str'>
['2', '12']

I will split 2.12
This number is string
['2', '12']

I will split 0.1
This number is float
I changed type to str: <class 'str'>
['0', '1']

I will split 0.1
This number is string
['0', '1']

I will split 1e-09
This number is float
I changed type to str: <class 'str'>
['1e-09']

I will split 0.000000001
This number is string
['0', '000000001']

I will split 0.000107
This number is float
I changed type to str: <class 'str'>
['0', '000107']

I will split 0.000107
This number is string
['0', '000107']

I will split 1.07e-05
This number is float
I changed type to str: <class 'str'>
['1', '07e-05']

I will split 0.0000107
This number is string
['0', '0000107']

I am using Python 3.8.6

luki
  • 197
  • 11
  • 3
    When a float is really small, `str(number)` uses exponential notation instead of using lots of digits after the decimal point. – Barmar Dec 06 '20 at 22:15
  • 1
    If you want more control over how the number is formatted, use a formatting function instead of `str()`. – Barmar Dec 06 '20 at 22:15
  • Also for really big floats. – quamrana Dec 06 '20 at 22:15
  • Can you clarify your question? There is no ``.`` to split on in ``1e-09``. What behaviour do you expect? Are you actually interested in splitting the string representation of a float, or in dividing a float into integral part and remainder? – MisterMiyagi Dec 06 '20 at 22:18
  • Thank you for ansars. – luki Dec 06 '20 at 22:38
  • @MisterMiyagi If you have 1e-6 for example and convert it to str, you should have 0.000001. Is not it? What i want to do: I have number: 0.000001234 and i want to got 0.0000012. That i can if i will split string and just make instruction number_after_point[:6]. That was my idea but when i have small numbers as float converted to string that does not work – luki Dec 06 '20 at 22:46
  • @Barmar I cant use format, because format round a number – luki Dec 06 '20 at 22:47
  • Ok. I understood that str(1.07e-3) always give me '1.07e-3' not '0.00107'. That means that i have to look for something like changing float representation. – luki Dec 07 '20 at 12:31

0 Answers0