A packed Binary-Coded Decimal (BCD) value backed by BYTES bytes.
Each byte encodes two decimal digits: the high nibble holds the more-significant
digit and the low nibble holds the less-significant digit. For example, the decimal
value 42 is stored as 0x42.
Bcd<0> is a valid, zero-sized type that stores no digits. try_into_* always
yields 0, and try_from_* accepts only 0 (any other value overflows). It
exists so the const-generic bound stays uniform; in most use cases BYTES >= 1
is expected.
Constructs a Bcd from a raw byte array already in packed BCD format.
Each nibble in bytes must be a valid decimal digit (0..=9). If any nibble
is in the range 10..=15, an InvalidBcdDigit error is returned containing
the offending nibble value.
use const_bcd::Bcd;
// Valid BCD bytes
let n = Bcd::<2>::from_bcd_bytes([0x12, 0x34]).unwrap();
assert_eq!(n.to_string(), "1234");
// Invalid: high nibble of first byte is 0xA
assert!(Bcd::<2>::from_bcd_bytes([0xA0, 0x00]).is_err());
// Invalid: low nibble of second byte is 0xF
assert!(Bcd::<2>::from_bcd_bytes([0x00, 0x0F]).is_err());
use const_bcd::Bcd;
let n = Bcd::<1>::try_from_u8(99).unwrap();
assert_eq!(n.to_string(), "99");
// 100 requires 3 digits but Bcd<1> holds at most 2
assert!(Bcd::<1>::try_from_u8(100).is_err());
use const_bcd::Bcd;
let n = Bcd::<2>::try_from_u16(9999).unwrap();
assert_eq!(n.to_string(), "9999");
// Bcd<1> can hold at most 99
assert!(Bcd::<1>::try_from_u16(100).is_err());
use const_bcd::Bcd;
let n = Bcd::<1>::try_from_u8(42).unwrap();
assert_eq!(n.try_into_u8(), Ok(42u8));
// A large Bcd<2> value won't fit in a u8
let big = Bcd::<2>::try_from_u16(1000).unwrap();
assert!(big.try_into_u8().is_err());
use const_bcd::Bcd;
// A value larger than u64::MAX that still fits in Bcd<10> (max 10^20 - 1).
let v = 20_000_000_000_000_000_000u128;
let n = Bcd::<10>::try_from_u128(v).unwrap();
assert_eq!(n.try_into_u128(), Ok(v));
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
Infallibly converts this BCD value to a primitive integer.
Available only for BYTES values whose maximum representable BCD
value is statically guaranteed to fit in the target primitive, so
this method cannot overflow.
use const_bcd::Bcd;
let n = Bcd::<4>::try_from_u32(42).unwrap();
assert_eq!(format!("{n}"), "42");
assert_eq!(format!("{n:8}"), " 42");
assert_eq!(format!("{n:08}"), "00000042");
assert_eq!(format!("{n:<8}"), "42 ");
assert_eq!(format!("{n:+}"), "+42");
// Zero displays as "0", not ""
let zero = Bcd::<2>::try_from_u16(0).unwrap();
assert_eq!(format!("{zero}"), "0");