mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
uucore: format: extendedbigdecimal: Implement From<f64>
Allows easier conversion.
This commit is contained in:
committed by
Sylvestre Ledru
parent
9355200901
commit
241e2291bd
@@ -25,6 +25,7 @@ use std::fmt::Display;
|
||||
use std::ops::Add;
|
||||
|
||||
use bigdecimal::BigDecimal;
|
||||
use num_traits::FromPrimitive;
|
||||
use num_traits::Zero;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -76,6 +77,28 @@ pub enum ExtendedBigDecimal {
|
||||
MinusNan,
|
||||
}
|
||||
|
||||
impl From<f64> for ExtendedBigDecimal {
|
||||
fn from(val: f64) -> Self {
|
||||
if val.is_nan() {
|
||||
if val.is_sign_negative() {
|
||||
ExtendedBigDecimal::MinusNan
|
||||
} else {
|
||||
ExtendedBigDecimal::Nan
|
||||
}
|
||||
} else if val.is_infinite() {
|
||||
if val.is_sign_negative() {
|
||||
ExtendedBigDecimal::MinusInfinity
|
||||
} else {
|
||||
ExtendedBigDecimal::Infinity
|
||||
}
|
||||
} else if val.is_zero() && val.is_sign_negative() {
|
||||
ExtendedBigDecimal::MinusZero
|
||||
} else {
|
||||
ExtendedBigDecimal::BigDecimal(BigDecimal::from_f64(val).unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ExtendedBigDecimal {
|
||||
pub fn zero() -> Self {
|
||||
Self::BigDecimal(0.into())
|
||||
|
||||
Reference in New Issue
Block a user