本文介绍了如何将科学记数法字符串转换为十进制记数法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 csv 文件中的所有科学记数法字符串find
和 convert
转换为十进制记数法,例如:
I'd like to find
and convert
all scientific notation strings in a csv file to decimal notation, e.g.:
1.0e-05 to 0.00001
我怎样才能在 ruby 中做到这一点?
How can I do that in ruby?
推荐答案
只需使用字符串转换即可.浮动的必要强制将自动完成:
Just use string conversion. The necessary coercion to float will be done automatically:
"%f" % "1.0e-05"
=> "0.000010"
# Which, behind the scenes is the same as:
"%f" % "1.0e-05".to_f
=> "0.000010"
根据需要进行调整以获得或多或少的准确性.例如:
Adjust as necessary to get more or less accuracy. For example:
"%.5f" % "1.0e-05"
=> "0.00001"
如果您想获得真正的幻想并在最后去掉不必要的零,这是一种方法.(希望有人会建议更优雅的东西;我想不出任何东西):
If you want to get real fancy and chop off unnecessary zeros at the end, here's one way. (Hopefully someone will suggest something more elegant; I couldn't think of anything):
("%.20f" % "1.0e-05").sub(/\.?0*$/, "")
=> "0.00001"
这篇关于如何将科学记数法字符串转换为十进制记数法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!