#fraction #print #align #fractional

fraction_list_fmt_align

Formats a list of arbitrary fractional numbers (either string or f32/f64) so that they are correctly aligned when printed line by line

5 releases

0.2.3 Dec 10, 2020
0.2.2 Dec 10, 2020
0.2.1 Dec 9, 2020
0.2.0 Dec 9, 2020
0.1.0 Dec 9, 2020

#124 in Command-line interface

Download history 10/week @ 2021-02-25 8/week @ 2021-03-04 5/week @ 2021-03-11 2/week @ 2021-03-18 23/week @ 2021-03-25 2/week @ 2021-04-01 9/week @ 2021-04-08 21/week @ 2021-04-15 8/week @ 2021-04-22 9/week @ 2021-04-29 3/week @ 2021-05-06 8/week @ 2021-05-13 2/week @ 2021-05-20 4/week @ 2021-05-27 5/week @ 2021-06-03 10/week @ 2021-06-10

52 downloads per month
Used in wambo

MIT license

12KB
155 lines

Rust library: fraction_list_fmt_align

Formats a list of arbitrary fractional numbers (either string or f32/f64) so that they are correctly aligned when printed line by line. It also removes unnecessary zeroes. This means that it rather returns "7" instead of "7.000000".

Input

a) either a list of formatted fractional number strings

b) or a list of f32/f64

Example

# Input
"-42"
"0.3214"
"1000"
"-1000.2"
"2.00000"

# Output
"  -42     "
"    0.3214"
" 1000     "
"-1000.2   "
"    2     "

Use case

If you want to write multiple fraction numbers of different lengths to the terminal or a file in an aligned/formatted way.

Difference to std::fmt

  • This is more flexible than println!()/format!() because it adjusts to a dynamic precision over multiple lines.
  • This removes unnecessary zeroes, i.e. "0.000" will become "0"

How to use

use fraction_list_fmt_align::{fmt_align_fraction_strings, FractionNumber, fmt_align_fractions};

fn main() {
    let input_1 = vec![
        "-42",
        "0.3214",
        "1000",
        "-1000.2",
    ];
    let aligned_1 = fmt_align_fraction_strings(&input_1);
    println!("{:#?}", aligned_1);

    // or
    let input_2 = vec![
        FractionNumber::F32(-42.0),
        FractionNumber::F64(0.3214),
        FractionNumber::F64(1000.0),
        FractionNumber::F64(-1000.2),
    ];
    let max_precision = 4;
    let aligned_2 = fmt_align_fractions(&input_2, max_precision);
    println!("{:#?}", aligned_2);
}

No runtime deps