Skip to content

Trying to format as hex still formats as decimal #4

@RecRanger

Description

@RecRanger

Example:

use thousands::{digits::ASCII_HEXADECIMAL, Separable, SeparatorPolicy};

pub fn display_hex_offset<T: std::fmt::LowerHex + num_traits::PrimInt + std::fmt::Display>(
    offset: T,
) -> String {
    let hex_display_policy = SeparatorPolicy {
        separator: "_",
        groups: &[4],
        digits: ASCII_HEXADECIMAL,
    };

    offset.separate_by_policy(hex_display_policy)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_display_hex_offset() {
        assert_eq!(display_hex_offset(0), "0"); // Passes
        assert_eq!(display_hex_offset(1), "1"); // Passes
        assert_eq!(display_hex_offset(10), "A"); // Fails, got "10"
        assert_eq!(display_hex_offset(15), "F"); // Fails, got "15"

        assert_eq!(display_hex_offset(16), "10");
        assert_eq!(display_hex_offset(4096), "1000");
        assert_eq!(display_hex_offset(268435455), "FFF_FFFF");
        assert_eq!(display_hex_offset(0xFFFF_FFFF_u64), "FFFF_FFFF");
    }
}

If I'm doing it wrong, you should add an example to the README.

Version = 0.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions