简体   繁体   English

用负数格式化int

[英]Format int with negative number

I have file that match this pattern: 我有符合此模式的文件:

Name-003
Name-002
Name-001
Name0000
Name0001
Name0002

And I need a way to format a string to have this pattern. 而且,我需要一种格式化字符串以具有此模式的方法。

If I use 如果我用

string.Format("Name{0:0000}", index)

It give me: 它给我:

Name-0003
Name-0002
Name-0001
Name0000
Name0001
Name0002

I need a way to specify the number of digit including the negative sign character. 我需要一种方法来指定包括负号字符在内的位数。 I want a solution with String.Format() 我想用String.Format()解决方案

You could look at the documentation for numeric formats (specifically the ; character): 您可以查看数字格式文档 (特别是;字符):

string.Format("Name{0:0000;-000}", index)

... or you could learn how to use an if statement to select between two format strings based on the value of index . ...或者您可以学习如何使用if语句根据index的值在两个格式字符串之间进行选择。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM