I need format number (-9_999 to 99_999) into fixed number of digits (5),

with leading zeros if needed, and this works fine:

```
let x = 89;
println!("{:0>5}", x);
```

but how to handle negative numbers?

`"{:0>5}", -70`

gives `00-70`

, while I need `-0070`

,

any way to get `00089`

for positive numbers and `-0070`

for negative one with format?

Or I have to add leading zeros manually ?

alice
2
```
fn main() {
println!("{:05}", 89);
println!("{:05}", -89);
}
```

```
00089
-0089
```

5 Likes

If you're looking for documentation, compare and contrast

```
let x = -89;
let formatted = if x < 0 {
format!("-{:05}", -x)
} else {
format!("{:05}", x)
};
println!("{}", formatted); // -00089
```

```
let x = -89;
let formatted = if x < 0 {
format!("-{:04}", -x)
} else {
format!("{:05}", x)
};
println!("{}", formatted); // -0089
```

```
let x: i32 = -89;
let formatted = if x < 0 {
format!("-{:0>4}", -x)
} else {
format!("{:0>5}", x)
};
println!("{}", formatted); // -0089
```

```
let x: i32 = -89;
let formatted = if x < 0 {
format!("-{:0>5}", -x)
} else {
format!("{:0>5}", x)
};
println!("{}", formatted); // -00089
```