[][src]Module steel_cent::formatting

Support for formatting and parsing of monetary values.

A style of formatting is described by a FormatSpec, which provides a function to wrap a Money or SmallMoney in a Display-implementing struct for use in format!, println!, etc.

use steel_cent::Money;
use steel_cent::currency::USD;
use steel_cent::formatting::{us_style, FormatSpec};
use steel_cent::formatting::FormatPart::*;

let money = Money::of_minor(USD, -123);
let custom_spec = FormatSpec::new(',', '.', vec![OptionalMinus, CurrencySymbol, Amount])
                      .with_short_symbol(USD, String::from("$"));
assert_eq!("total: ($1.23)",
           format!("total: {}", us_style().display_for(&money)));
assert_eq!("total: -$1.23",
           format!("total: {}", custom_spec.display_for(&money)));

If you just need a formatted money String without additional context, use the format fn directly.

A FormatSpec can also be used to create a Parser for strings of the specified format.

Structs

FormatSpec

A specification of a currency format.

MoneyDisplay

A Display wrapper for a monetary amount and a FormatSpec. Obtained from FormatSpec::display_for.

ParseError

Returned when a string cannot be parsed.

Parser

A Parser is created from a FormatSpec and parses strings conforming to that spec.

Enums

FormatPart

Elements that can appear in a FormatSpec template.

Traits

FormattableMoney
ParseableMoney

Functions

format

Given a FormatSpec and a monetary value, returns a formatted string.

france_style

A format for France, using the Euro symbol in place of "EUR."

generic_style

A generic (US- and UK-friendly) readable style with no short symbol mappings.

uk_style

A format for the United Kingdom, using the pound sign in place of "GBP."

us_style

A format for the United States, using the dollar sign in place of "USD" and parentheses for negative amounts.