Module fraction::display

source ·
Expand description

Implementation of fmt::Display for GenericFraction and Sign structures

Complete support of the std::fmt formatting syntax with exceptions for alternate integer types (hexadecimal, octal, binary)

There are a couple of extensions on top of std::fmt, which are

  • Alternate flag (#) is used to enable trailing zeroes output
  • Negative sign (-) is used to suppress sign output (even negative)

Otherwise, the standard formatting is followed as is.

§Examples

use fraction::prelude::Fraction;

let fraction = Fraction::from(1.75);

assert_eq!("7/4", format!("{}", fraction));
assert_eq!("1.75", format!("{:.4}", fraction));
assert_eq!("1.7500", format!("{:#.4}", fraction));
assert_eq!("-1.75", format!("{:.2}", -fraction));
assert_eq!("1.75", format!("{:-.2}", -fraction));

Structs§

  • Same as fmt::Formatter, but allowing to change flags so we can reuse it in recursive format implementations

Functions§