Crates.io | fraction_list_fmt_align |
lib.rs | fraction_list_fmt_align |
version | 0.3.1 |
source | src |
created_at | 2020-12-09 14:09:32.183755 |
updated_at | 2023-01-16 14:53:59.360057 |
description | Formats a list of arbitrary fractional numbers (either string or f32/f64) so that they are correctly aligned when printed line by line. |
homepage | https://github.com/phip1611/fraction_list_fmt_align |
repository | https://github.com/phip1611/fraction_list_fmt_align |
max_upload_size | |
id | 321171 |
size | 16,854 |
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".
a) either a list of formatted fractional number strings
b) or a list of f32/f64
# Input
"-42"
"0.3214"
"1000"
"-1000.2"
"2.00000"
# Output
" -42 "
" 0.3214"
" 1000 "
"-1000.2 "
" 2 "
If you want to write multiple fraction numbers of different lengths to the terminal or a file in an aligned/formatted way.
std::fmt
println!()/format!()
because it adjusts to a dynamic precision
over multiple lines.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);
}
The MSRV is 1.56.1
.