【问题标题】:How to print F32 with 1 dec place and a fixed width in rust如何在 rust 中打印 1 dec 位置和固定宽度的 F32
【发布时间】:2021-12-17 10:51:44
【问题描述】:

尝试复制以下 C printf 调用

printf("%6.1f %6.1f %6.1f\n", x, y, z);

在 rust 中,我在文档中找不到它。我需要 6 位宽,小数点后 1 位。

我第一次很难找到我需要的东西。

【问题讨论】:

    标签: rust printing formatting width


    【解决方案1】:

    std::fmt 的文档中记录了格式化字符串。

    %6.1f 的等价物是 {:6.1}

    println!("{:6.1} {:6.1} {:6.1}", x, y, z);
    

    Playground

    【讨论】:

    • 哇...我不知道std::fmt...超级!谢谢!
    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 2012-01-17
    • 2013-08-28
    • 2015-07-24
    • 2020-03-23
    • 2012-08-28
    • 2015-06-04
    相关资源
    最近更新 更多