python string to decimal

Solutions on MaxInterview for python string to decimal by the best coders in the world

showing results for - "python string to decimal"
Ian
27 Jan 2019
1import decimal
2list = ["3","4.5","6"]
3string = "6"
4list2 = []
5control = Decimal(7)
6for i in list:
7    list2.append(Decimal(i))
8decimal_string = Decimal(string)
9#Control is to show that a decimal number has a class of <class 'decimal.Decimal'>
10print(type(control))
11#<class 'decimal.Decimal'>
12print(type(decimal_string))
13for i in list2:
14    print(i)
15    print(type(i))
16    #<class 'decimal.Decimal'>
17#As shown above, converting strings and arrays to a decimal is quite easy.
18#I hope this helps you to solve your problem :)