3 releases (1 stable)

1.0.0 May 11, 2019
0.1.1 May 31, 2018
0.1.0 May 31, 2018

#64 in Value formatting

Download history 24046/week @ 2024-03-14 20693/week @ 2024-03-21 25726/week @ 2024-03-28 25468/week @ 2024-04-04 29931/week @ 2024-04-11 22387/week @ 2024-04-18 19973/week @ 2024-04-25 21492/week @ 2024-05-02 20070/week @ 2024-05-09 20405/week @ 2024-05-16 18209/week @ 2024-05-23 19848/week @ 2024-05-30 19641/week @ 2024-06-06 29897/week @ 2024-06-13 23290/week @ 2024-06-20 16900/week @ 2024-06-27

93,585 downloads per month
Used in 117 crates (28 directly)

Apache-2.0

18KB
465 lines

Latest Version Documentation

This crate adds a tool to format a number in an arbitrary base from 2 to 36.

This is a light crate, without any dependency.

For primitive signed integers (i8 to i128, and isize), negative values are formatted as the two’s complement representation.

There is also one specific function for each radix that does not already exists in the standard library, e.g. radix_3 to format a number in base 3.

Get started

Add the crate in the cargo manifest:

radix_fmt = "1"

Import radix in scope, and you are ready to go:

use radix_fmt::radix;

Examples

use radix_fmt::*;

let n = 35;

// Ouput: "z"
println!("{}", radix(n, 36));
// Same ouput: "z"
println!("{}", radix_36(n));

You can use the alternate modifier to capitalize the letter-digits:

use radix_fmt::radix;

let n = 35;

// Ouput: "Z"
println!("{:#}", radix(n, 36));

FAQ

  • Which digits are used when the base is superior to 10?

    This crate uses the letters in alphabetic order. That is why the maximum base is 36: it uses all the digits and all the letters of the alphabet.

  • Among the functions that format in a specific base, why are some missing? For example there are radix_7 and radix_9, but not radix_8.

    All the numbers in range [2, 36] are represented except 2, 8, 10 and 16 because they already exist in the standard library through binary, octal, decimal (regular) and hexadecimal formatting.

  • What if I want to use the capitalized letters as digits?

    Use the alternate modifier {:#}.

  • Why does the formatting of negative numbers give a weird result?

    Just as in the standard library, when a number is formatted in a non-decimal base, the two’s complement representation is used. That means that the number is casted to the unsigned version (for example, for an i8 the following number is used: n as u8).

No runtime deps