python float函数字符串转换浮点数 保留位数
展开全部
int函数能够
(1)把符合数学格式的数字型字符串转换成整数
(2)把浮点数转换成整数,但是只是简单的取整,而非四舍五入。
举例:
1
aa
=
int("124")
#Correct
2
print
"aa
=
",
aa
#result=124
3
bb
=
int(123.45)
#correct
4
print
"bb
=
",
bb
#result=123
5
cc
=
int("-123.45")
#Error,Can't
Convert
to
int
6
print
"cc
=
",cc
7
dd
=
int("34a")
#Error,Can't
Convert
to
int
8
print
"dd
=
",dd
9
ee
=
int("12.3")
#Error,Can't
Convert
to
int
10
print
ee
11
二、float函数将整数和字符串转换成浮点数。
举例:
1
aa
=
float("124")
#Correct
2
print
"aa
=
",
aa
#result
=
124.0
3
bb
=
float("123.45")
#Correct
4
print
"bb
=
",
bb
#result
=
123.45
5
cc
=
float(-123.6)
#Correct
6
print
"cc
=
",cc
#result
=
-123.6
7
dd
=
float("-123.34")
#Correct
8
print
"dd
=
",dd
#result
=
-123.34
9
ee
=
float('123v')
#Error,Can't
Convert
to
float
10
print
ee
三、str函数将数字转换成字符
举例:
1
aa
=
str(123.4)
#Correct
2
print
aa
#result
=
'123.4'
3
bb
=
str(-124.a)
#SyntaxError:
invalid
syntax
4
print
bb
5
cc
=
str("-123.45")
#correct
6
print
cc
#result
=
'-123.45'
7
dd
=
str('ddd')
#correct
8
print
dd
#result
=
ddd
9
ee
=
str(-124.3)
#correct
10
print
ee
#result
=
-124.3
(1)把符合数学格式的数字型字符串转换成整数
(2)把浮点数转换成整数,但是只是简单的取整,而非四舍五入。
举例:
1
aa
=
int("124")
#Correct
2
"aa
=
",
aa
#result=124
3
bb
=
int(123.45)
#correct
4
"bb
=
",
bb
#result=123
5
cc
=
int("-123.45")
#Error,Can't
Convert
to
int
6
"cc
=
",cc
7
dd
=
int("34a")
#Error,Can't
Convert
to
int
8
"dd
=
",dd
9
ee
=
int("12.3")
#Error,Can't
Convert
to
int
10
ee
11
二、float函数将整数和字符串转换成浮点数。
举例:
1
aa
=
float("124")
#Correct
2
"aa
=
",
aa
#result
=
124.0
3
bb
=
float("123.45")
#Correct
4
"bb
=
",
bb
#result
=
123.45
5
cc
=
float(-123.6)
#Correct
6
"cc
=
",cc
#result
=
-123.6
7
dd
=
float("-123.34")
#Correct
8
"dd
=
",dd
#result
=
-123.34
9
ee
=
float('123v')
#Error,Can't
Convert
to
float
10
ee
三、str函数将数字转换成字符
举例:
1
aa
=
str(123.4)
#Correct
2
aa
#result
=
'123.4'
3
bb
=
str(-124.a)
#SyntaxError:
invalid
syntax
4
bb
5
cc
=
str("-123.45")
#correct
6
cc
#result
=
'-123.45'
7
dd
=
str('ddd')
#correct
8
dd
#result
=
ddd
9
ee
=
str(-124.3)
#correct
10
ee
#result
=
-124.3
展开全部
int函数能够
(1)把符合数学格式的数字型字符串转换成整数
(2)把浮点数转换成整数,但是只是简单的取整,而非四舍五入。
(1)把符合数学格式的数字型字符串转换成整数
(2)把浮点数转换成整数,但是只是简单的取整,而非四舍五入。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
round(float(x), 6) 你要保留结尾的0的话,不能存成float数据,float会自动去掉末尾的0
你需要保存你的结果为string或者decimal.Decimal
string的话:
"%.6f" % float(x)
decimal的话:
import decimal
decimal.Decimal("%.6f" % float(x))
你需要保存你的结果为string或者decimal.Decimal
string的话:
"%.6f" % float(x)
decimal的话:
import decimal
decimal.Decimal("%.6f" % float(x))
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询