.NET中的字符串格式:将整数转换为固定宽度的字符串? [英] String format in .NET: convert integer to fixed width string?

查看:50
本文介绍了.NET中的字符串格式:将整数转换为固定宽度的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET/C#中有一个整数,我想将其转换为特定格式的字符串.

I have an int in .NET/C# that I want to convert to a specifically formatted string.

如果值为1,我希望字符串为"001".

If the value is 1, I want the string to be "001".

10 ="010".

10 = "010".

116 ="116".

116 = "116".

等...

我正在寻找字符串格式,但到目前为止没有成功.我的值也不会超过999.

I'm looking around at string formatting, but so far no success. I also won't have values over 999.

推荐答案

看看例如:

int i = 40;

string s = i.ToString().PadLeft(3, '0'); 

s =="040"

s == "040"

这篇关于.NET中的字符串格式:将整数转换为固定宽度的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆