简体   繁体   English

使用 fmtlib,当值为负时,零填充数值更短,我可以调整这种行为吗?

[英]Using fmtlib, zero padded numerical value are shorter when the value is negative, can I adapt this behaviour?

I am using fmtlib to format strings and numeric values but I have problem with negative integers.我正在使用fmtlib来格式化字符串和数值,但我对负整数有问题。 When I pad the value with zeroes, I expect a consistent number of zero no matter the sign of the value.当我用零填充值时,无论值的符号如何,我都希望有一个一致的零数。

For instance, using a padding of 4, I would like the following:例如,使用 4 的填充,我想要以下内容:

  • 2 to be returned as "0002" 2 返回为“0002”
  • -2 to be returned as "-0002" -2 将作为“-0002”返回

fmtlib's default behaviour is to take into account a prefix lenght (ie the sign "-") into the padded length which means that -2 is returned as "-002" fmtlib 的默认行为是在填充长度中考虑前缀长度(即符号“-”),这意味着 -2 返回为“-002”

Here is an example:下面是一个例子:

#include <iostream>
#include "fmt/format.h"

int main()
{
    std::cout << fmt::format("{:04}", -2) << std::endl;
}

will output: -002将输出: -002

Is there a way to toggle this behaviour or a different way to zero-fill values to get my expected result?有没有办法切换这种行为或以不同的方式填充零值以获得我的预期结果?

Thanks for any help,谢谢你的帮助,

There's certainly nothing in the documentation for either fmt or Python's str.format (which fmt's syntax is based on). fmt 或 Python 的str.format (fmt 的语法基于)的文档中肯定没有任何内容。 Both only state that padding is "sign-aware".两者都只说明填充是“符号感知”的。

This question asks for the same functionality for Python's str.format .这个问题要求 Python 的str.format具有相同的功能。 The accepted answer there is to move the length to an argument, and make it one larger if the number is negative.那里接受的答案是将长度移到参数中,如果数字为负,则将其增大一。 Translating that to C++:将其转换为 C++:

for (auto x : { -2, 2 }) {
    fmt::print("{0:0{1}}\n", x, x < 0 ? 5 : 4 ); // prints -0002 and 0002
}

To break the format syntax down:分解格式语法:

{0:0{1}}
 │ │ └ position of the argument with the length
 │ └── "pad with zeros"
 └──── position of the argument with the value

https://godbolt.org/z/5xz7T9 https://godbolt.org/z/5xz7T9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于负零浮点值? - Uses for negative zero floating point value? 如何区分使用数值和Clang使用#define? - How can I distinguish the use of a numerical value and the use of a #define with Clang? 为什么使用int i的值; 未定义的行为,但使用rand()的值不是? - Why is using the value of int i; undefined behaviour but using the value of rand() is not? 当左侧操作数为负值时,为什么左移操作会调用未定义行为? - Why does left shift operation invoke Undefined Behaviour when the left side operand has negative value? 使用 fmtlib 格式化 ISO 时间 - Formatting ISO time using fmtlib 如何正确地将数组的所有元素设置为负值? - How can I correctly set all elements of an array to a negative value? 一个补码架构上的负零行为? - Behaviour of negative zero on a one's complement architecture? NAN - &gt;区分除零和指数具有非常大的负值 - NAN -> distinguish between a division by zero and an exponent with a very large negative value 当一个为负数时,如何将两个16位值组合为一个32位值? - How can I combine two 16-bit values into a single 32-bit value when either one is negative? 与正零 (+0.0) 相比,负零 (-0.0) 的行为 - Behaviour of negative zero (-0.0) in comparison with positive zero (+0.0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM