Function fraction::division::divide_to_ascii_vec

source ·
pub fn divide_to_ascii_vec<I>(
    dividend: I,
    divisor: I,
    precision: usize,
    trail_zeroes: bool,
) -> Result<Vec<u8>, DivisionError>
where I: Clone + GenericInteger,
Expand description

Divide a fraction into a Vec<u8> of ASCII(utf8) chars

WARNING: Negative numbers as arguments are not supported.

  • Makes only one allocation for the resulting vector
  • Does not round the last digit

Calculates the resulting vector length first, allocates it, then makes the division and puts the result into the preallocated vector. Uses divide_integral and divide_rem functions internally.

§Examples

use fraction::division::divide_to_ascii_vec;

assert_eq! (divide_to_ascii_vec(2, 4, 2, false).unwrap(), vec![48, 46, 53]);  // "0.5" in ascii
assert_eq! (divide_to_ascii_vec(2, 4, 2, true).unwrap(), vec![48, 46, 53, 48]);  // "0.50" in ascii
assert_eq! (divide_to_ascii_vec(5, 7, 16, false).unwrap(), vec![48, 46, 55, 49, 52, 50, 56, 53, 55, 49, 52, 50, 56, 53, 55, 49, 52, 50]);  // "0.7142857142857142" in ascii
assert_eq! (divide_to_ascii_vec(1, 3, 3, false).unwrap(), vec![48, 46, 51, 51, 51]);  // "0.333" in ascii