转换成INT字符串的字符一定长度 [英] convert int into string with certain length of char

查看:235
本文介绍了转换成INT字符串的字符一定长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果标题不清晰,生病尝试以及在这里解释它。我有一大堆的整数,从1到999,我需要将这些转换成字符串,但是当我做到这一点,我需要他们3个字符长。因此,举例来说,如果我有:

  INT I1 = 45;当我把那转化为字符串,我需要这种



然后: 045或类似的,如果我有8的整数,那么,将有变成008,如果有什么有3个地方如143,那么它也只是输出为143这是容易做到?



感谢您提前反应。 :)


解决方案

 字符串输出= someInt.ToString(000); 

如果您想使它更有活力,你会做这样的事情。



  //其中'长'是3 
字符串输出= someInt.ToString(新的字符串(0,长度));
//或
字符串输出= i.ToString()PadLeft(长度,'0');


If the title wasn't clear, ill try to explain it well here. I have a bunch of integers, ranging from 1 to 999, and i need to convert these into strings, but when i do that, i need them to be 3 characters long. so for instance, if i had:

int i1 = 45;

then when i turned that into a string, i'd need this: "045" or similarly, if i had an int of 8 then that would have to turn into "008", and if anything had 3 places, such as 143, then it would just be outputted as 143. is this easily possible?

Thanks for responses in advance. :)

解决方案

string output = someInt.ToString("000");

If you would like to make it more dynamic, you would do something like this

// where 'length' is 3
string output = someInt.ToString(new string('0', length));
// or 
string output = i.ToString().PadLeft(length, '0');

这篇关于转换成INT字符串的字符一定长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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