Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a float to string?

Tags:

rust

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 = ..... 
like image 742
Philipp Ludwig Avatar asked Sep 06 '17 22:09

Philipp Ludwig


People also ask

Can you convert a float to a string in Python?

We can convert float to a string easily using str() function.

Can enter float be converted to a string?

You can use the strconv package's FormatFloat() function to convert the float into an string value. FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec.

Can we convert float to string in C++?

We can convert float and double to string using the C++11 std::to_string() function.

How do I convert a float list to a string in Python?

The most Pythonic way to convert a list of floats fs to a list of strings is to use the one-liner fs = [str(x) for x in fs] . It iterates over all elements in the list fs using list comprehension and converts each list element x to a string value using the str(x) constructor.


1 Answers

Sometimes, the answer is easy: to_string().

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

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);

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

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()!

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!

Advanced formatting

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);

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

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);

This will result in "3.1415926e6".

like image 121
Lukas Kalbertodt Avatar answered Sep 29 '22 10:09

Lukas Kalbertodt