Skip to main content

parse_decimal

Function parse_decimal 

pub fn parse_decimal<T>(
    s: &str,
    precision: u8,
    scale: i8,
) -> Result<<T as ArrowPrimitiveType>::Native, ArrowError>
where T: DecimalType,
Expand description

Parses the string representation of a decimal number into the unscaled native value of a decimal type with the given precision and scale.

The accepted syntax is:

[whitespace] [+|-] digits [. [digits]] [(e|E) [+|-] digits] [whitespace]

or the same with the integer digits omitted (e.g. .5), as long as at least one digit is present in the mantissa. ASCII whitespace is trimmed from both ends. The exponent is applied before scaling, so 1.5e2 and 150 parse identically.

Fractional digits beyond scale are not stored but round the result half away from zero (e.g. 1.005 at scale 2 is 101, -1.005 is -101). Negative scales are supported and round the integer part in the same way (e.g. 150 at scale -2 is 2).

Returns an error if the input is not a valid decimal string, or if the result does not fit the given precision.

ยงExample

assert_eq!(parse_decimal::<Decimal128Type>("123.45", 10, 2).unwrap(), 12345);
assert_eq!(parse_decimal::<Decimal128Type>("1.005", 10, 2).unwrap(), 101);
assert_eq!(parse_decimal::<Decimal128Type>("1.5e2", 10, 0).unwrap(), 150);
assert!(parse_decimal::<Decimal128Type>("1234.5", 5, 2).is_err()); // does not fit