| View previous topic :: View next topic |
| Author |
Message |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Wed Sep 30, 2009 12:55 pm Post subject: c# code improvements. |
|
|
Is there a better way of doing this.
| Code: | private string FormatFix(int input)
{
if (input < 10)
{
return "00" + input;
}
else if (input < 100)
{
return "0" + input;
}
return input.ToString();
} |
| Code: |
private bool isNumeric(string input)
{
foreach (char c in input)
{
if (!char.IsDigit(c))
{
return false;
}
}
return true;
} |
_________________
Intel over amd yes.
Last edited by NINTENDO on Wed Sep 30, 2009 1:25 pm; edited 1 time in total |
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Wed Sep 30, 2009 1:22 pm Post subject: |
|
|
I think this would be a better way to do it:
| Code: |
private bool IsNumeric (string input)
{
try
{
int.Parse(input);
}
catch(FormatExcpetion ex)
{return false;}
return true;
}
|
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Wed Sep 30, 2009 1:29 pm Post subject: |
|
|
| Odecey wrote: | I think this would be a better way to do it:
| Code: |
private bool IsNumeric (string input)
{
try
{
int.Parse(input);
}
catch(FormatExcpetion ex)
{return false;}
return true;
}
|
|
so like
| Code: | int lol;
return int.TryParse(input, out lol); |
_________________
Intel over amd yes. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Oct 01, 2009 1:24 am Post subject: Re: c# code improvements. |
|
|
| JesusLovesYou wrote: | | Code: | | private string FormatFix(int input) |
| I bet C# has some way to adjust the width and the fill, but since I'm no C# coder, I don't know how to do this in C# :p
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Thu Oct 01, 2009 4:22 am Post subject: |
|
|
I guess you can make the FormatFix more general, like this:
| Code: |
private string FormatFix(int input,int numOfDigits)
{
string output = "";
int dif = input.ToString().Length -numOfDigits;
if(dif>0)
for(int i = 0;dif>i;i++)
output += "0";
return output += input.ToString();
}
|
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Thu Oct 01, 2009 7:42 am Post subject: |
|
|
| Odecey wrote: | I guess you can make the FormatFix more general, like this:
| Code: |
private string FormatFix(int input,int numOfDigits)
{
string output = "";
int dif = input.ToString().Length -numOfDigits;
if(dif>0)
for(int i = 0;dif>i;i++)
output += "0";
return output += input.ToString();
}
|
|
that way I have to know the number of digitis? Keep in mind that the value of the integer is not hardcoded.
_________________
Intel over amd yes. |
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Thu Oct 01, 2009 10:26 am Post subject: |
|
|
| JesusLovesYou wrote: | | Odecey wrote: | I guess you can make the FormatFix more general, like this:
| Code: |
private string FormatFix(int input,int numOfDigits)
{
string output = "";
int dif = input.ToString().Length -numOfDigits;
if(dif>0)
for(int i = 0;dif>i;i++)
output += "0";
return output += input.ToString();
}
|
|
that way I have to know the number of digitis? Keep in mind that the value of the integer is not hardcoded. |
Perhaps you were confused by a mistake I made, it should actually be:
| Code: |
private string FormatFix(int input,int numOfDigits)
{
string output = "";
int dif =numOfDigits- input.ToString().Length;// Originally I had set numOfDigits as subtrahend.
if(dif>0)
for(int i = 0;dif>i;i++)
output += "0";
return output += input.ToString();
}
|
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Oct 01, 2009 1:49 pm Post subject: |
|
|
This is not what I really meant.. More like this: | Code: | stringstream ss;
ss.width( 8 );
ss.fill( '0' );
ss.print( number );
return ss.str(); |
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Thu Oct 01, 2009 4:16 pm Post subject: |
|
|
| Jani wrote: | This is not what I really meant.. More like this: | Code: | stringstream ss;
ss.width( 8 );
ss.fill( '0' );
ss.print( number );
return ss.str(); |
|
What are you on about? This:
| Code: | | FormatFix(number, 8); |
Should give the same output as your pseudocode, and is just as easy.
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
SCRN Grandmaster Cheater Supreme
Reputation: 1
Joined: 02 Sep 2007 Posts: 1319 Location: BOX
|
Posted: Thu Oct 01, 2009 8:06 pm Post subject: |
|
|
There's a built in function that already does what you're trying to do.
In VB.NET you can use Format or PadLeft:
Format(NameOfIntegerVariable, "000")
NameOfIntegerVariable.ToString.PadLeft(3, "0")
In C#:
Strings.Format(NameOfIntegerVariable, "000");
NameOfIntegerVariable.ToString.PadLeft(3, "0");
Note I used a converter for the C# code. I usually use VB.NET and I'm not sure how accurate the converter is. VB.NET and C#.NET are really similar though so the code should work fine.
_________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Oct 02, 2009 1:23 am Post subject: |
|
|
| Odecey wrote: | | Should give the same output as your pseudocode, and is just as easy. | I meant my code for the body of the FormatFix, not as a replacement. I find padding zeroes in a loop like that very ugly.
Kumori Guardian: that's more like it.. It might be the thing I'm talking about. Let's say .Format() it's...
I'm sure that C# can fill the string to given length when converting an integer to a string. Knowing nothing about C#, it's very fun to give any advice :P Well, I do know how things should be done..
EDIT: I finally did a bit Googling, well more MSDN browsing, anyway here's how it's done:
| Code: | private string FormatFix(int input)
{
return String.Format("{0:D3}", input);
} | It fills any string to length of 3 padding zeroes in front of the string if needed. Change the 3 to eg. 8 if you want 8 digits... Ha, I do know C#! :p Also remember to catch exceptions. If you want to add an another param to indicate the length, you know how to work with strings, don't you?
|
|
| Back to top |
|
 |
|