是否可以在Rust中打印一个用千位分隔符格式化的数字?

问题描述 投票:13回答:4

例如

println!("{}", 10_000_000);

结果是

10000000

而我想把它格式化为类似的东西

10,000,000

我经历了the fmt module documentation,但没有什么可以涵盖这种特殊情况。我觉得这样的事情会起作用

println!("{:,i}", 10_000_000);

但它会引发错误

invalid format string: expected `}`, found `,`
rust
4个回答
2
投票

num_format crate将为您解决这个问题。添加您的语言环境,它将发挥魔力。


5
投票

没有,也可能不会。

根据您所处的位置,千位分隔符也可能像1,00,00,0001.000.000,000或其他变体一样工作。

本地化不是stdlib的工作,加上format!主要是在编译时处理的(虽然公平地说它可以很容易地放在它的运行时部分),并且你不想在项目中硬盘化一个语言环境。


1
投票

另一个解决方法是使用separator crate,它在float,integer和size类型上实现.separated_string()方法。这是一个例子:

extern crate separator;
use separator::Separatable;

fn main() {
    let x1: u16 = 12345;
    let x2: u64 = 4242424242;
    let x3: u64 = 232323232323;
    println!("Unsigned ints:\n{:>20}\n{:>20}\n{:>20}\n", x1.separated_string(), x2.separated_string(), x3.separated_string());

    let x1: i16 = -12345;
    let x2: i64 = -4242424242;
    let x3: i64 = -232323232323;
    println!("Signed ints:\n{:>20}\n{:>20}\n{:>20}\n", x1.separated_string(), x2.separated_string(), x3.separated_string());


    let x1: f32 = -424242.4242;
    let x2: f64 = 23232323.2323;
    println!("Floats:\n{:>20}\n{:>20}\n", x1.separated_string(), x2.separated_string());


    let x1: usize = 424242;
    // let x2: isize = -2323232323;  // Even though the docs say so, the traits seem not to be implemented for isize
    println!("Size types:\n{:>20}\n", x1.separated_string());        
}

这给你以下输出:

Unsigned ints:
              12,345
       4,242,424,242
     232,323,232,323

Signed ints:
             -12,345
      -4,242,424,242
    -232,323,232,323

Floats:
         -424,242.44
     23,232,323.2323

Size types:
             424,242

请注意,对齐像这样的浮点数并不容易,因为separated_string()返回一个字符串。但是,这是获得分开数字的相对快速的方法。


0
投票

关于自定义功能,我玩了这个,这里有一些想法:

use std::str;

fn main() {
    let i = 10_000_000i;
    println!("{}", decimal_mark1(i.to_string()));
    println!("{}", decimal_mark2(i.to_string()));
    println!("{}", decimal_mark3(i.to_string()));
}

fn decimal_mark1(s: String) -> String {
    let bytes: Vec<_> = s.bytes().rev().collect();
    let chunks: Vec<_> = bytes.chunks(3).map(|chunk| str::from_utf8(chunk).unwrap()).collect();
    let result: Vec<_> = chunks.connect(" ").bytes().rev().collect();
    String::from_utf8(result).unwrap()
}

fn decimal_mark2(s: String) -> String {
    let mut result = String::with_capacity(s.len() + ((s.len() - 1) / 3));
    let mut i = s.len();
    for c in s.chars() {
        result.push(c);
        i -= 1;
        if i > 0 && i % 3 == 0 {
            result.push(' ');
        }
    }
    result
}

fn decimal_mark3(s: String) -> String {
    let mut result = String::with_capacity(s.len() + ((s.len() - 1) / 3));
    let first = s.len() % 3;
    result.push_str(s.slice_to(first));
    for chunk in s.slice_from(first).as_bytes().chunks(3) {
        if !result.is_empty() {
            result.push(' ');
        }
        result.push_str(str::from_utf8(chunk).unwrap());
    }
    result
}

围栏:http://is.gd/UigzCf

欢迎评论,他们都没有感觉真的很好。

© www.soinside.com 2019 - 2024. All rights reserved.