How to write a python script, that converts a string format number into 'real' number: integer, float.

0 Robert Trumpet · September 18, 2015
 I need to implement python built-in type convert function: 


The code should convert the following 3 strings into real values:
   (1) "7343345"
   (2) "-87634"
   (3) "98.783"


   What python functions are not allowed:
1.  the type convert function itself: int(), float()


In other words....
Define a function, that takes a string format number as input, and return the number itself. 
e.g. def stringToNumber(str):
         #your code here
           return number

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 sfolje 0 · September 19, 2015
oh, i misunderstand question. my bad.
0 sfolje 0 · September 18, 2015
I dont believe it's possible to convert string to integer without some kind of build-it functions.

You can get for example first or whichever number in string format with:
"7343345"[0]
"7343345"[i]

Or you can get id number of character ( character = string with 1 letter)
ord("a")
ord("7")
0 Abdullah Sukkar · October 12, 2015
when ? write this code

num1 = 127.899
num2 = 3465.148
num3 = 3.776
num4 = 264.821
num5 = 88.08
print(format(num1,'.2f'),'\n',format(num2,'.2f'),'\n',format(num3,'.2f'),'\n',format(num4,'.2f'),'\n',num5)
the numbers does not come under each others
please tell me what is the wrong
0 Abdullah Sukkar · October 12, 2015
when ? write this code

num1 = 127.899
num2 = 3465.148
num3 = 3.776
num4 = 264.821
num5 = 88.08
print(format(num1,'.2f'),'\n',format(num2,'.2f'),'\n',format(num3,'.2f'),'\n',format(num4,'.2f'),'\n',num5)
the numbers does not come under each others
please tell me what is the wrong
0 Bernard Pyc0d333 Parah · September 19, 2015
Agree with Clark 

.isdigit() definitely does the trick

you can also use a third-party module like fastnumbers if you wish

you might have to download the module.


from fastnumbers import fast_real
# you just use pass the number as an argument into the fast real function
fast_real("7343345")

# you can also get the data type thus

type(fast_real("7343345"))


and then you could also always use the yaml method of doing that

import yaml
a = "27733"
a = yaml.load(a)
# you get the real value


def convertToDigit():
...     a = "2343"
...     a = yaml.load(a)
...     print (a)

0 sfolje 0 · September 19, 2015
@Clark Kentanabi 
I dont believe your function converts string < class 'str' > to number < class 'float' > or < class 'int' > . Sooner or later you will need some build-in function or third party one (@Bernard Pyc0d333 Parah).
Else function would be quite simple using
float("27.733")
0 Bernard Pyc0d333 Parah · September 19, 2015
I don't really get your point please
0 Bernard Pyc0d333 Parah · October 13, 2015
they do come down for me.
0 Abdullah Sukkar · October 13, 2015
sir , 
i want them to come down each other and every line starts from the same start point not like this way :
127.90 
  3465.15 
  3.78 
  264.82 
  88.08
0 Halcyon Abraham Ramirez · October 13, 2015
ugh doesn't isdigit return a boolean?

and if you do this


def stringToNumber(str):

        return str.replace("'", "")






print(type(stringToNumber("7343345")))



output is:



so it wasn't converted :/

and isdigit() returns a boolean there is no actual conversion :/
  • 1
  • 2

Python

128,068 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator