Set output percission to 3

This commit is contained in:
Alexander Rosenberg 2022-10-06 14:58:15 -07:00
parent c496d12aa2
commit 085bdd44da
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730

View File

@ -1,13 +1,13 @@
use std::io::{self, Write}; use std::io::{self, Write};
fn read_float(prompt: &str) -> f32 { fn read_float(prompt: &str) -> f32 {
print!("{}", prompt); print!("{prompt}");
let _ = io::stdout().flush(); let _ = io::stdout().flush();
let mut line = String::new(); let mut line = String::new();
let _ = io::stdin().read_line(&mut line); let _ = io::stdin().read_line(&mut line);
let flt = line[..line.len() - 1].parse::<f32>(); let flt = line[..line.len() - 1].parse::<f32>();
if flt.is_err() { if flt.is_err() {
println!("Invalid number: '{}'", line); println!("Invalid number: '{line}'");
return read_float(prompt); return read_float(prompt);
} }
return flt.unwrap(); return flt.unwrap();
@ -23,7 +23,7 @@ fn main() {
let lb_weight = read_float("Enter weight (pounds): "); let lb_weight = read_float("Enter weight (pounds): ");
let cm_height = in_height * 2.54; let cm_height = in_height * 2.54;
let kg_weight = lb_weight / 2.20462; let kg_weight = lb_weight / 2.20462;
println!("Your metric height: {}cm", cm_height); println!("Your metric height: {cm_height:.3}cm");
println!("Your metric weight: {}kg", kg_weight); println!("Your metric weight: {kg_weight:.3}kg");
println!("Your BMI: {}", calculate_bmi(kg_weight, cm_height)); println!("Your BMI: {:.3}", calculate_bmi(kg_weight, cm_height));
} }