pub trait ToBase {
type Err;
// Required method
fn to_base(&self, base: Base, add_prefix: bool) -> Result<String, Self::Err>;
}
Expand description
Facilitates formatting an integer into a Base.
Required Associated Types§
Required Methods§
Sourcefn to_base(&self, base: Base, add_prefix: bool) -> Result<String, Self::Err>
fn to_base(&self, base: Base, add_prefix: bool) -> Result<String, Self::Err>
Formats self into a base, specifying whether to add the base prefix or not.
Tries converting self
into a NumberWithBase and then formats into the provided base by
using the Debug implementation.
§Example
use alloy_primitives::U256;
use cast::base::{Base, ToBase};
// Any type that implements ToBase
let number = U256::from(12345);
assert_eq!(number.to_base(Base::Decimal, false).unwrap(), "12345");
assert_eq!(number.to_base(Base::Hexadecimal, false).unwrap(), "3039");
assert_eq!(number.to_base(Base::Hexadecimal, true).unwrap(), "0x3039");
assert_eq!(number.to_base(Base::Binary, true).unwrap(), "0b11000000111001");
assert_eq!(number.to_base(Base::Octal, true).unwrap(), "0o30071");