使用格式时,如何在正浮点数之前强制使用“+”号!宏观?
How do I force the '+' sign before positive floats when using the format! macro?
我想要由 format!
宏格式化的右对齐浮点数,并且符号始终可见。使用 syntax specification 我设计了以下格式:
format!("{:>10+.1}", 23.3434);
但是我遇到了编译错误:
error: invalid format string: expected `'}'`, found `'+'`
--> src/main.rs:2:21
|
2 | let x = format!("{:>10+.1}", 23.3434);
| ^^^^^^^^^^^
我正在使用 Rust 1.25.0。
规范明确给出了顺序 [[fill]align][sign]['#']['0'][width]
with:
align := '<' | '^' | '>'
sign := '+' | '-'
因此您不能在 >
和 +
之间输入数字,并且宽度在符号之后:
format!("{:>10+.1}", 23.3434);
这呈现为 " +23.3"
。鉴于:
format!("{:>+010.1}", 23.3434);
呈现为 +0000023.3
。
不过,为了便于维护,我建议使用
format!("{:>+0width$.prec$}", 23.3434, width=10, prec=1);
我想要由 format!
宏格式化的右对齐浮点数,并且符号始终可见。使用 syntax specification 我设计了以下格式:
format!("{:>10+.1}", 23.3434);
但是我遇到了编译错误:
error: invalid format string: expected `'}'`, found `'+'`
--> src/main.rs:2:21
|
2 | let x = format!("{:>10+.1}", 23.3434);
| ^^^^^^^^^^^
我正在使用 Rust 1.25.0。
规范明确给出了顺序 [[fill]align][sign]['#']['0'][width]
with:
align := '<' | '^' | '>'
sign := '+' | '-'
因此您不能在 >
和 +
之间输入数字,并且宽度在符号之后:
format!("{:>10+.1}", 23.3434);
这呈现为 " +23.3"
。鉴于:
format!("{:>+010.1}", 23.3434);
呈现为 +0000023.3
。
不过,为了便于维护,我建议使用
format!("{:>+0width$.prec$}", 23.3434, width=10, prec=1);