Git Product home page Git Product logo

thousands-rs's Introduction

thousands

Build Status Crates.io License: MIT License: Apache 2.0

Provides a trait, Separable, for formatting numbers with separators between the digits. Typically this will be used to add commas or spaces every three digits from the right, but can be configured via a SeparatorPolicy.

Examples

The simplest way to use the library is with trait Separable’s method separate_with_commas method, which does what it sounds like:

use thousands::Separable;

 assert_eq!(   12345  .separate_with_commas(),  "12,345" );
 assert_eq!( (-12345) .separate_with_commas(), "-12,345" );
 assert_eq!(    9876.5.separate_with_commas(),   "9,876.5" );

There are also methods separate_with_spaces, separate_with_dots, and separate_with_underscores, in case you, your culture, or your file format prefer those separators.

However, it's also possible to pass a policy for different behavior:

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

let policy = SeparatorPolicy {
    separator: ',',
    groups:    &[3, 2],
    digits:    digits::ASCII_DECIMAL,
};

assert_eq!( 1234567890.separate_by_policy(policy), "1,23,45,67,890" );

Usage

It’s on crates.io, so you can add

[dependencies]
thousands = "0.2.0"

to your Cargo.toml.

This crate supports Rust version 1.22 and newer.

thousands-rs's People

Contributors

tov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

iune

thousands-rs's Issues

please add support for underscore too

Please add num.separate_with_underscores() too, for converting for instance 1234 to 1_234. I know it could be done with a policy but a shortcut would be easier.

Rationale: you can also use _ for literals, e.g. let num = 1_234; , so it seems obvious that we might want to see a number like this in a human-readable format.

Trying to format as hex still formats as decimal

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.