I have 2 Strings namely
string1 = '1000000000.0'
string2 = '1000000000.09'
from decimal import Decimal
Decimal(string1).normalize()
gives
Decimal('1E+9')
and
Decimal(string2).normalize()
gives
Decimal('1000000000.09')
Which i actually want like the above.
so how can string1 be as Decimal(1000000000) instead of Decimal('1E+9') and string2 as Decimal('1000000000.09') which i get in currently.!!
You probably want to use string formatting. This gives you very close control over how your data is represented - see the documentation on str.format
for a full (and a little complex for a beginner) explanation.
To answer your specific request:
from decimal import Decimal
string1 = '1000000000.0'
string2 = '1000000000.09'
fstr = '{:10.0f} {:12.2f}'
print(fstr.format(Decimal(string1), Decimal(string2)))
You should find the output is
1000000000 1000000000.09
To make your output easier to understand by humans, you might want to change fstr
to '{:10,.0f} {:12,.2f}'
- the commas indicate that the integer part is to be split into comma-separated groups of three, and you should see the output
1,000,000,000 1,000,000,000.09
This Functions Do the Trick
def DecimalHandler(string):
if re.match('^\d*[.]?\d*$',string):
Frst = string.split('.')[0]
try:
secnd = string.split('.')[1]
secnd = re.sub('[0]+$','',secnd)
if secnd != '' :
secnd = '.'+secnd
else:
secnd = secnd
except IndexError:
secnd = ''
OutPut_Str = Frst+secnd
else:
print("...")
OutPut_Str = string
return(OutPut_Str)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.