pub struct NumStr<const B: u8 = 10, const U: bool = false, const R: usize = 0, const M: usize = 0, T = usize>(/* private fields */);
Expand description
Number to string
§Generic
-
B
: the base of the number, should be within the range2..=16
. Default is 10. -
U
: whether to use uppercase for hex. Default is lowercase (false).For float number,
U
means whether only to reserve the integer part. -
R
: the resize length of the string. The overflow part will be truncated, and the insufficient part will be filled with ‘0’. Default is 0, or no resize. -
M
: the minimum length of the string, if the length of the string is less thanM
, fill with ‘0’. Default is 0, or no minimum. For signed numberM
will be ignored. -
T
: the underlying type of the number. Default isusize
.
§Panic
- Invalid base (== 0 or > 16)
Implementations§
Source§impl<T> NumStr<10, false, 0, 0, T>
impl<T> NumStr<10, false, 0, 0, T>
Sourcepub fn new_default(inner: T) -> Self
pub fn new_default(inner: T) -> Self
Source§impl NumStr<10, false, 0, 0, u8>
impl NumStr<10, false, 0, 0, u8>
Sourcepub fn hex_byte_default(inner: u8) -> NumStr<16, false, 2, 0, u8>
pub fn hex_byte_default(inner: u8) -> NumStr<16, false, 2, 0, u8>
§Create a new NumStr
with the given number, mostly for encoding bytes to hex.
With default settings of B
, U
, R
, M
:
B
:16
U
:false
R
:2
M
:0
See NumStr
for details.
§Notice
For negative number, R
, M
will not make sense
§Examples
let nums = vec![0x11, 0x45, 0x14, 0x19, 0x19, 0x81, 0x00]
.into_iter()
.map(NumStr::hex_byte_default);
assert_eq!(nums.to_string_ext(), "11451419198100");
Source§impl<const B: u8, const U: bool, const R: usize, const M: usize, T> NumStr<B, U, R, M, T>
impl<const B: u8, const U: bool, const R: usize, const M: usize, T> NumStr<B, U, R, M, T>
Sourcepub fn hexadecimal(self) -> NumStr<16, U, R, M, T>
pub fn hexadecimal(self) -> NumStr<16, U, R, M, T>
Convert to hexadecimal representation.
Sourcepub fn set_custom_base<const NB: u8>(self) -> NumStr<NB, U, R, M, T>
pub fn set_custom_base<const NB: u8>(self) -> NumStr<NB, U, R, M, T>
Set custom base.
The valid range is 2..=16
Sourcepub fn set_uppercase<const NU: bool>(self) -> NumStr<B, NU, R, M, T>
pub fn set_uppercase<const NU: bool>(self) -> NumStr<B, NU, R, M, T>
Set uppercase / lowercase of the number.
Default is lowercase
Note: only works for base > 10
Sourcepub fn set_resize_len<const NR: usize>(self) -> NumStr<B, U, NR, M, T>
pub fn set_resize_len<const NR: usize>(self) -> NumStr<B, U, NR, M, T>
Set whether to resize the string to len
length.
The overflow part will be truncated, and the insufficient part will be filled with ‘0’
Default is not resize
Note: see Vec::resize
for details
Sourcepub fn set_minimum_len<const NM: usize>(self) -> NumStr<B, U, R, NM, T>
pub fn set_minimum_len<const NM: usize>(self) -> NumStr<B, U, R, NM, T>
Set the minimum length of the string.
The insufficient part will be filled with ‘0’.
Default is not minimum
Note: if set Self::should_resize
, the minimum length will be ignored
Source§impl<const B: u8, const U: bool, const R: usize, const M: usize> NumStr<B, U, R, M, f32>
impl<const B: u8, const U: bool, const R: usize, const M: usize> NumStr<B, U, R, M, f32>
Sourcepub fn set_integer_only<const NU: bool>(self) -> NumStr<B, NU, R, M, f32>
pub fn set_integer_only<const NU: bool>(self) -> NumStr<B, NU, R, M, f32>
Set integer only mode.
Default disable.
Source§impl<const B: u8, const U: bool, const R: usize, const M: usize> NumStr<B, U, R, M, f64>
impl<const B: u8, const U: bool, const R: usize, const M: usize> NumStr<B, U, R, M, f64>
Sourcepub fn set_integer_only<const NU: bool>(self) -> NumStr<B, NU, R, M, f64>
pub fn set_integer_only<const NU: bool>(self) -> NumStr<B, NU, R, M, f64>
Set integer only mode.
Default disable.