fn from_str(input: &str) -> Result<Self, Self::Err> {
        let parts: Vec<&str> = input.split('.').collect();
        match parts.len() {
            1 => {
                let whole = parts[0]
                    .parse::<u128>()
                    .map_err(|_| StdError::generic_err("Error parsing whole"))?;

                let whole_as_atomics = whole
                    .checked_mul(DECIMAL_FRACTIONAL)
                    .ok_or_else(|| StdError::generic_err("Value too big"))?;
                Ok(Decimal(whole_as_atomics))
            }
            2 => {
                let whole = parts[0]
                    .parse::<u128>()
                    .map_err(|_| StdError::generic_err("Error parsing whole"))?;
                let fractional = parts[1]
                    .parse::<u128>()
                    .map_err(|_| StdError::generic_err("Error parsing fractional"))?;
                let exp = (18usize.checked_sub(parts[1].len())).ok_or_else(|| {
                    StdError::generic_err("Cannot parse more than 18 fractional digits")
                })?;
                let fractional_factor = 10u128
                    .checked_pow(exp.try_into().unwrap())
                    .ok_or_else(|| StdError::generic_err("Cannot compute fractional factor"))?;

                let whole_as_atomics = whole
                    .checked_mul(DECIMAL_FRACTIONAL)
                    .ok_or_else(|| StdError::generic_err("Value too big"))?;
                let atomics = whole_as_atomics
                    .checked_add(fractional * fractional_factor)
                    .ok_or_else(|| StdError::generic_err("Value too big"))?;
                Ok(Decimal(atomics))
            }
            _ => Err(StdError::generic_err("Unexpected number of dots")),
        }
    }
/// Converts the decimal string to a Decimal
/// Possible inputs: "1.23", "1", "000012", "1.123000000"
/// Disallowed: "", ".23"
///
/// This never performs any kind of rounding.
/// More than 18 fractional digits, even zeros, result in an error.


This never performs any kind of rounding.
More than 18 fractional digits, even zeros, result in an error.
This never performs any kind of rounding . More than 18 fractional digits even zeros result in an error .
fn from_str(input: &str) -> Result<Self, Self::Err> {
        let parts: Vec<&str> = input.split('.').collect();
        match parts.len() {
            1 => {
                let whole = parts[0]
                    .parse::<u128>()
                    .map_err(|_| StdError::generic_err("Error parsing whole"))?;
                let whole_as_atomics = whole
                    .checked_mul(DECIMAL_FRACTIONAL)
                    .ok_or_else(|| StdError::generic_err("Value too big"))?;
                Ok(Decimal(whole_as_atomics))
            }
            2 => {
                let whole = parts[0]
                    .parse::<u128>()
                    .map_err(|_| StdError::generic_err("Error parsing whole"))?;
                let fractional = parts[1]
                    .parse::<u128>()
                    .map_err(|_| StdError::generic_err("Error parsing fractional"))?;
                let exp = (18usize.checked_sub(parts[1].len())).ok_or_else(|| {
                    StdError::generic_err("Cannot parse more than 18 fractional digits")
                })?;
                let fractional_factor = 10u128
                    .checked_pow(exp.try_into().unwrap())
                    .ok_or_else(|| StdError::generic_err("Cannot compute fractional factor"))?;
                let whole_as_atomics = whole
                    .checked_mul(DECIMAL_FRACTIONAL)
                    .ok_or_else(|| StdError::generic_err("Value too big"))?;
                let atomics = whole_as_atomics
                    .checked_add(fractional * fractional_factor)
                    .ok_or_else(|| StdError::generic_err("Value too big"))?;
                Ok(Decimal(atomics))
            }
            _ => Err(StdError::generic_err("Unexpected number of dots")),
        }
    }
fn from_str ( input : & str ) -> Result < Self , Self :: Err > { let parts : Vec < & str > = input . split ( '.' ) . collect ( ) ; match parts . len ( ) { 1 => { let whole = parts [ 0 ] . parse :: < u128 > ( ) . map_err ( | _ | StdError :: generic_err ( " " ) ) ? ; let whole_as_atomics = whole . checked_mul ( DECIMAL_FRACTIONAL ) . ok_or_else ( | | StdError :: generic_err ( " " ) ) ? ; Ok ( Decimal ( whole_as_atomics ) ) } 2 => { let whole = parts [ 0 ] . parse :: < u128 > ( ) . map_err ( | _ | StdError :: generic_err ( " " ) ) ? ; let fractional = parts [ 1 ] . parse :: < u128 > ( ) . map_err ( | _ | StdError :: generic_err ( " " ) ) ? ; let exp = ( 18usize . checked_sub ( parts [ 1 ] . len ( ) ) ) . ok_or_else ( | | { StdError :: generic_err ( " " ) } ) ? ; let fractional_factor = 10u128 . checked_pow ( exp . try_into ( ) . unwrap ( ) ) . ok_or_else ( | | StdError :: generic_err ( " " ) ) ? ; let whole_as_atomics = whole . checked_mul ( DECIMAL_FRACTIONAL ) . ok_or_else ( | | StdError :: generic_err ( " " ) ) ? ; let atomics = whole_as_atomics . checked_add ( fractional * fractional_factor ) . ok_or_else ( | | StdError :: generic_err ( " " ) ) ? ; Ok ( Decimal ( atomics ) ) } _ => Err ( StdError :: generic_err ( " " ) ) , } }
Converts the decimal string to a Decimal
Possible inputs: "1.23", "1", "000012", "1.123000000"
Disallowed: "", ".23"
Converts the decimal string to a Decimal Possible inputs : " 1 . 23 " " 1 " " 000012 " " 1 . 123000000 " Disallowed : " " " . 23 "
input
&str
input
&str