Skip to main content

float_to_decimal

Function float_to_decimal 

Source
pub fn float_to_decimal<D>(
    input: f64,
    precision: u8,
    scale: i8,
) -> Result<D::Native, ArrowError>
where D: DecimalType, D::Native: DecimalCast,
Expand description

Converts a floating point value to the unscaled integer representation of a decimal.

Scales the input by 10^scale and rounds half away from zero. Returns an error if the precision or scale is invalid, or the result does not fit the native type or the requested precision. Non-finite inputs are rejected. Use float_to_decimal_opt when the reason for failure is not needed.

use arrow_array::types::Decimal32Type;
use arrow_cast::cast::float_to_decimal;

assert_eq!(float_to_decimal::<Decimal32Type>(12.345, 5, 2).unwrap(), 1235);
assert!(float_to_decimal::<Decimal32Type>(12345.678, 5, 2).is_err());