Add five zéros on the left of int
-
Hello, I would add five zéros on the left of int, like this : 1------------->0000000001 345----------->0000000345 65576--------->0000065576 How i can make this? thank you verry mutch.
int number = 1;
string numberString = "00000" + number.ToString();
MessageBox.Show(numberString); // '000001':bob: Kristian Sixhoej "You can always become better." - Tiger Woods
-
int number = 1;
string numberString = "00000" + number.ToString();
MessageBox.Show(numberString); // '000001':bob: Kristian Sixhoej "You can always become better." - Tiger Woods
-
Hello, I would add five zéros on the left of int, like this : 1------------->0000000001 345----------->0000000345 65576--------->0000065576 How i can make this? thank you verry mutch.
I think the best way to do this is with a custom formatter, that way you'll be able to reuse it. Google it. I think what you're looking for though is this:
class Program { static void Main(string[] args) { Int32 number = 234; String s = number.ToString().PadLeft(9, '0'); Console.WriteLine(s); Console.ReadLine(); } }
Scott P"Simplicity carried to the extreme becomes elegance."
-Jon Franklin -
Hello, I would add five zéros on the left of int, like this : 1------------->0000000001 345----------->0000000345 65576--------->0000065576 How i can make this? thank you verry mutch.
There are many, many ways... What you want to do according to your question: "00000" + number.ToString() or number.ToString("'00000'0") or string.Format("'00000'{0}", number) or new String('0', 5) + number.ToString() or number.ToString().Insert(0, "00000") What you want to do according to your examples: String.Format("{0:0000000000}", number) or number.ToString("0000000000") or number.ToString().PadLeft(10, '0')
Despite everything, the person most likely to be fooling you next is yourself.
-
Hello, I would add five zéros on the left of int, like this : 1------------->0000000001 345----------->0000000345 65576--------->0000065576 How i can make this? thank you verry mutch.
Add the number "1000000000" to the int, that would give you this; 1------------->10000000001 345----------->10000000345 65576--------->10000065576 Now, convert them to a string, and loose the first character. That would give you these strings; 10000000001------------->0000000001 10000000345------------->0000000345 10000065576------------->0000065576 Enjoy :)
I are troll :)
-
Add the number "1000000000" to the int, that would give you this; 1------------->10000000001 345----------->10000000345 65576--------->10000065576 Now, convert them to a string, and loose the first character. That would give you these strings; 10000000001------------->0000000001 10000000345------------->0000000345 10000065576------------->0000065576 Enjoy :)
I are troll :)
-
Add the number "1000000000" to the int, that would give you this; 1------------->10000000001 345----------->10000000345 65576--------->10000065576 Now, convert them to a string, and loose the first character. That would give you these strings; 10000000001------------->0000000001 10000000345------------->0000000345 10000065576------------->0000065576 Enjoy :)
I are troll :)
Brilliant =)
-
Brilliant =)
-
There are many, many ways... What you want to do according to your question: "00000" + number.ToString() or number.ToString("'00000'0") or string.Format("'00000'{0}", number) or new String('0', 5) + number.ToString() or number.ToString().Insert(0, "00000") What you want to do according to your examples: String.Format("{0:0000000000}", number) or number.ToString("0000000000") or number.ToString().PadLeft(10, '0')
Despite everything, the person most likely to be fooling you next is yourself.
wow... I just thought 2 from these.
-
Add the number "1000000000" to the int, that would give you this; 1------------->10000000001 345----------->10000000345 65576--------->10000065576 Now, convert them to a string, and loose the first character. That would give you these strings; 10000000001------------->0000000001 10000000345------------->0000000345 10000065576------------->0000065576 Enjoy :)
I are troll :)
there are too many ways of doing same thing... :-D cool. are you an assembley language programmer before ? ;P
-
:omg: Not the most elegant and simple, obvious. It's just a fun question that has lots of possible solutions. Some solutions even make code-obfuscation irrelevant :laugh:
I are troll :)
There sure are some interresting solutions. Here's an almost completely useless way of doing it: String.Join(null,number.ToString().ToCharArray().Reverse().Select(c=>c.ToString()).Concat(new int[10].Select(i=>i.ToString())).Take(10).Reverse().ToArray()) ;)
Despite everything, the person most likely to be fooling you next is yourself.
-
there are too many ways of doing same thing... :-D cool. are you an assembley language programmer before ? ;P
Then it would probably have been a more hardcore solution: char[] c = new char[10]; for (int i = 9; i >= 0; number /= 10) c[i--] = (char)('0' + number % 10); string result = new String(c); ;)
Despite everything, the person most likely to be fooling you next is yourself.
-
Hello, I would add five zéros on the left of int, like this : 1------------->0000000001 345----------->0000000345 65576--------->0000065576 How i can make this? thank you verry mutch.
-
There sure are some interresting solutions. Here's an almost completely useless way of doing it: String.Join(null,number.ToString().ToCharArray().Reverse().Select(c=>c.ToString()).Concat(new int[10].Select(i=>i.ToString())).Take(10).Reverse().ToArray()) ;)
Despite everything, the person most likely to be fooling you next is yourself.
LINQ! :-D I haven't done much with Linq yet, but I'll take the time for it this weekend. It's turning up at more and more places, and most people agree that it's a good thing. ..and that would be a better idea than to write a recursive method to padd zeroes, wouldn't it? :laugh:
I are troll :)
-
there are too many ways of doing same thing... :-D cool. are you an assembley language programmer before ? ;P
-
Then it would probably have been a more hardcore solution: char[] c = new char[10]; for (int i = 9; i >= 0; number /= 10) c[i--] = (char)('0' + number % 10); string result = new String(c); ;)
Despite everything, the person most likely to be fooling you next is yourself.
OK. I give up... :-D Hey you forgot
asm
{}
;P
-
Noes, I learned it in "Amos Basic", using GWBasic examples :-\ It's been over fifteen years, and there are still days that I'm having trouble with even the most basic statement :laugh:
I are troll :)
Eddy Vluggen wrote:
It's been over fifteen years
wow long time huh... :) I have started with QBasic. But I still remember those college days when in exams they ask some silly things like. *) write a function to swap 2 variables without using 3rd one or references. *) draw a pascal triangle without using array (recurrsion : damn thing) X|
-
Eddy Vluggen wrote:
It's been over fifteen years
wow long time huh... :) I have started with QBasic. But I still remember those college days when in exams they ask some silly things like. *) write a function to swap 2 variables without using 3rd one or references. *) draw a pascal triangle without using array (recurrsion : damn thing) X|
Things haven't changed much, my roommate is in college and learning C# using the book "Head First C#". It's got some basic examples as to be expected, and they're gonna build two games during class. One arcade-type, another rpg-type. It looks better when you see the graphics, but is just as much fun as doing a Snakes-game in basic :-D ..and yeah, I do miss those logical puzzles from school sometimes. I don't miss being sent out of class though :suss:
I are troll :)
-
OK. I give up... :-D Hey you forgot
asm
{}
;P
No, that wasn't assembly code. If inline asm was supported, it might look more like this:
string result;
unsafe {
char* p = stackalloc char[10];
asm {
lea esi, number
mov eax, [si]
lea edi, p
add edi, 014
std
mov cx, 0a
.digit
xor edx, edx
div dword 0a
xchg eax, edx
add ax, 030
stosw
xchg eax, edx
loop digit
}
result = new String(c);
}:)
Despite everything, the person most likely to be fooling you next is yourself.