Abstract: format conversion of a number
The result:
convert integer to string:
10 <type 'int'>
10 <type 'str'>
convert integer to float
10 <type 'int'>
10.0 <type 'float'>
convert float to scientific notation
Scientific with 4 bit: 3.345e+04
Scientific with 4 bit: 3.345e-11
Scientific : 3.453434242e+17
convert decimal to binary
Decimal: 233
binary: 0b11101001
binary: 11101001
hexadecimal: 0xe9
convert binary to decimal
Decimal: 143
hexadecimal: 0x8f
The script:
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 2 08:16:54 2015
@author: yuan
"""
print 'convert integer to string:'
a=10
print a, type(a)
a=str(a)#to string
print a, type(a)
print '\nconvert integer to float'
a=int(10)
print a, type(a)
a=float(10)
print a, type(a)
print '\nconvert float to scientific notation'
a="%.4g" % 33453.4342420
print 'Scientific with 4 bit:',a
a="%.4g" % 0.0000000000334534342420
print 'Scientific with 4 bit:',a
a="%.10g" % 345343424200000000
print 'Scientific :',a
print '\nconvert decimal to binary'
a=233
print 'Decimal:', a
print 'binary:', bin(a)
print 'binary:', format(a, 'b')
print 'hexadecimal:', hex(a)
print '\nconvert binary to decimal'
a=10001111
print 'Decimal:', int(str(a), 2)
print 'hexadecimal:', hex(int(str(a), 2))
No comments:
Post a Comment