没有标准
f64::try_from(i128)
.
您可以在互惠后通过检查来实现自己的功能
as
胁迫。
fn try_to_f64(v: i128) -> Result<f64, &'static str> {
let attempt = v as f64;
(attempt as i128 == v)
.then_some(attempt)
.ok_or("precision loss in i128 to f64 conversion")
}
fn main() {
for v in [
123_456_789,
123_456_789_123,
123_456_789_123_456,
123_456_789_123_456_789,
] {
println!("{} ~~> {:?}", v, try_to_f64(v));
}
}
/*
123456789 ~~> Ok(123456789.0)
123456789123 ~~> Ok(123456789123.0)
123456789123456 ~~> Ok(123456789123456.0)
123456789123456789 ~~> Err("precision loss in i128 to f64 conversion")
*/