[][src]Macro display_as::display_floats_as

macro_rules! display_floats_as {
    ($format:ty, $e:expr, $after_e:expr, $e_cost:expr, $power_ten:expr) => { ... };
}

Inconveniently implement DisplayAs for floats for a new Format.

This is inconvenient because we want to enable pretty formatting of both large and small numbers in whatever markup language we are using. The first argument of the macro is the format that wants implementation of DisplayAs for floats.

For partial documentation of the other files, see Floating::fmt_with. However, I think some examples for HTML will most easily define the other arguments.

struct HTML;
use display_as::{Format, format_as};
impl Format for HTML {
   fn escape(f: &mut ::std::fmt::Formatter, mut s: &str) -> Result<(), ::std::fmt::Error> {
       f.write_str(s) // for example I skip escaping...
   }
   fn mime() -> mime::Mime { return mime::TEXT_HTML_UTF_8; }
   fn this_format() -> Self { HTML }
}
display_as::display_floats_as!(HTML, "×10<sup>", "</sup>", 3, Some("10<sup>"));
fn main() {
  assert_eq!(&format_as!(HTML, 1e3), "1000");
  assert_eq!(&format_as!(HTML, 3e4), "30000");
  assert_eq!(&format_as!(HTML, 1e5), "10<sup>5</sup>");
  assert_eq!(&format_as!(HTML, 2e5), "2×10<sup>5</sup>");
  assert_eq!(&format_as!(HTML, 1e6), "10<sup>6</sup>");
}