铸造浮动到没有科学符号的字符串 [英] Casting float to string without scientific notation

查看:160
本文介绍了铸造浮动到没有科学符号的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浮点数:

  fl = 0.000005 

转换为 String as str(fl)=='5e-06'。但是,我想要将其转换为 str(fl)='0.000005'以导出到 CSV 目的。 p>

如何实现?

解决方案

使用

  fl = 0.00005 
s = str('%8.5f'%fl)
print s,type(s)

提供

  0.00005< type'str'> 

如果您不想要额外的数字,请使用%g

  fl = 0.0005 
s = str('%g'%fl)
打印s,类型

fl = 0.005
s = str('%g'%fl)
print s,type(s)

提供

  0.0005< type'str'> 
0.005< type'str'>


The float:

fl = 0.000005

casts to String as str(fl)=='5e-06'. however, I want it to cast as str(fl)='0.000005' for exporting to CSV purposes.

How do I achieve this?

解决方案

Use

fl = 0.00005
s = str('%8.5f' % fl)
print s, type(s)

Gives

0.00005 <type 'str'>

In case you want no extra digits, use %g

fl = 0.0005
s = str('%g' % fl)
print s, type(s)

fl = 0.005
s = str('%g' % fl)
print s, type(s)

Gives

0.0005 <type 'str'>
0.005 <type 'str'>

这篇关于铸造浮动到没有科学符号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆