如何在 Rust 中向上或向下舍入一个数字?
How to round a number up or down in Rust?
如何设置楼层数或天花板数?我尝试使用 round
箱子,但要么它不起作用,要么我用错了。
use round::round_down;
fn main() {
println!("{}", round_down(5.5f64, 0));
}
这会打印 5.5
但应该打印 5
.
我的 Cargo.toml
文件包含以下内容:
[dependencies]
round = "0.1.0"
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
}
fn main() {
let mut num = 5.5_f64;
num = num.floor();
print!("{}", num); // 5
}
如何设置楼层数或天花板数?我尝试使用 round
箱子,但要么它不起作用,要么我用错了。
use round::round_down;
fn main() {
println!("{}", round_down(5.5f64, 0));
}
这会打印 5.5
但应该打印 5
.
我的 Cargo.toml
文件包含以下内容:
[dependencies]
round = "0.1.0"
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
}
fn main() {
let mut num = 5.5_f64;
num = num.floor();
print!("{}", num); // 5
}