gpt4 book ai didi

rust - 如何在Rust中将数字四舍五入?

转载 作者:行者123 更新时间:2023-12-03 11:25:26 28 4
gpt4 key购买 nike

如何设置地板号或天花板号?我尝试使用round crate ,但它无法正常工作或使用错误。

use round::round_down;

fn main() {
println!("{}", round_down(5.5f64, 0));
}

这将打印 5.5,但应打印 5

我的 Cargo.toml文件包含以下内容:
[dependencies]
round = "0.1.0"

最佳答案

如评论中所述,您可以使用:floorceil

fn main() {
let num_32 = 3.14159_f32;

println!("{}", num_32.floor()); // Output: 3
println!("{}", num_32.ceil()); // Output: 4


let num_64 = 3.14159_f64;

println!("{}", num_64.floor()); // Output: 3
println!("{}", num_64.ceil()); // Output: 4
}

关于rust - 如何在Rust中将数字四舍五入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62144550/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com