If i write this:
let x: f32 = 10.0;
println!("let x: f32 = {}", x);
I get a string
let x: f32 = 10
But i want to output 10.0 (with point!!!)
I need it for rust codegen. But this is not a valid rust code.
If i write this:
let x: f32 = 10.0;
println!("let x: f32 = {}", x);
I get a string
let x: f32 = 10
But i want to output 10.0 (with point!!!)
I need it for rust codegen. But this is not a valid rust code.
println!("let x: f32 = {:.1}", x};
For details, check out std::fmt
This cut precision to 1.
I need a more general method because i need to generate rs file with array of floats
for example:
static A: [f32; 5] = [1.0, 2.01, 3.57656, 4.0, 5.55435];
I don't know an answer to your exact question. But perhaps your requirement is satisfied by the following statement:
println!("let x = {}f32", x);
Thanks!
Does this statement really work ?
I cannot find a better way to deal with your requirement, here are two alternative ways:
Yes - this is exactly what i need.
Thanks!
Relative question:
How i can print float in this form 1.0e-40 ?
Check out dtoa
which serde_json uses for printing floating point numbers in e-notation.
extern crate dtoa;
use std::io;
fn main() {
print!("let x = ");
dtoa::write(io::stdout(), 1e-40).unwrap();
}
I found that format!("{:e}", 0.111) do the job
https://stackoverflow.com/a/53640992/11639569
Using the precision format specifier is the correct answer, but to print all available precision , simply refrain from specifying the number of digits to display:
// prints 1
println!("{:.}", 1_f64);
// prints 0.000000000000000000000000123
println!("{:.}", 0.000000000000000000000000123_f64);
This topic was automatically closed 30 days after the last reply. We invite you to open a new topic if you have further questions or comments.