如何将浮点数转换为字符串? [英] How can I convert a float to string?

查看:50
本文介绍了如何将浮点数转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将浮点值转换为字符串?无论出于何种原因,我能找到的文档和所有在线资源都只与相反的方式有关.

How can a float value be converted to a String? For whatever reason, the documentation and all online sources I can find are only concerned with the other way around.

let value: f32 = 17.65;
let value_as_str: String = ..... 

推荐答案

有时,答案很简单:to_string().

let pi = 3.1415926;
let s = pi.to_string();  // : String

<小时>

背景

创建可读的字符串表示"的基础在 fmt 模块.这个模块中最重要的特征可能是 Display.Display 是对可以格式化为面向用户的字符串的类型的抽象(几乎正是您想要的).通常 Display trait 被 println!() 和朋友使用.因此,您已经可以使用 format!() 宏将浮点数转换为字符串:


Background

The foundation for "creating a readable string representation of something" is in the fmt module. Probably the most important trait in this module is Display. Display is an abstraction over types that can be formatted as a user-facing string (pretty much exactly what you want). Usually the Display trait is used by println!() and friends. So you can already convert your float to string with the format!() macro:

let s = format!("{}", pi);

但是还有别的东西:ToString 特性.这个 trait 讨论了可以转换为 String 的类型.现在,有一个神奇的实现:

But there is something else: the ToString trait. This trait talks about types that can be converted to a String. And now, there is a magic implementation:

impl<T> ToString for T 
    where T: Display + ?Sized

这意味着:每个实现了Display的类型也自动实现了ToString!因此,您可以简单地编写 your_value.to_string()!

This means: every type which implements Display also automatically implements ToString! So instead of writing format!("{}", your_value) you can simply write your_value.to_string()!

虽然这些通配符实现非常有用且用途广泛,但它们有一个缺点:查找方法要困难得多.正如您所指出的,f32 的文档 根本没有提到 to_string().这不是很好,但这是一个已知问题.我们正在努力改善这种情况!

While these wildcard implementations are extremely useful and versatile, they have one disadvantage: finding methods is much harder. As you point out, the documentation of f32 doesn't mention to_string() at all. This is not very good, but it is a known issue. We're trying to improve this situation!

to_string() 方法使用默认格式选项,因此它等效于 format!("{}", my_value).但有时,您想调整值如何转换为字符串.为此,您必须使用 format!()fmt 格式说明符的全部功能.您可以在 模块文档.一个例子:

The to_string() method uses the default formatting options, so it's equivalent to format!("{}", my_value). But sometimes, you want to tweak how the value is converted into a string. To do that, you have to use format!() and the full power of the fmt format specifier. You can read about those in the module documentation. One example:

let s = format!("{:.2}", pi);

这将导致一个字符串正好小数点后两位数("3.14").

This will result in a string with exactly two digits after the decimal point ("3.14").

如果您想使用科学符号将浮点数转换为字符串,您可以使用{:e}(或{:E}code>) 格式说明符,对应于 LowerExp(或UpperExp) 特质.

If you want to convert your float into a string using scientific notation, you can use the {:e} (or {:E}) format specifier which corresponds to the LowerExp (or UpperExp) trait.

let s = format!("{:e}", pi * 1_000_000.0);

这将导致 "3.1415926e6".

这篇关于如何将浮点数转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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