Reputation: 1365
As you can see in the code it use several if conditions to achieve the required result. I only include part of the code which has the same pattern. At the moment the IdString value can be in between “0” – “99999”. Later it can be even bigger. If that happen we have to change the code again.
private string GetPrefix(string IdString)
{
string returnString = string.Empty;
if (IdString >= 0 && IdString < 1000)
{
returnString = "00000";
}
else if (IdString >= 1000 && IdString < 2000)
{
returnString = "00001";
}
else if (IdString >= 2000 && IdString < 3000)
{
returnString = "00002";
}
..........//repeate same pattern ..........
else if (IdString >= 49000 && IdString < 50000)
{
returnString = "00049";
}
.........//repeate same pattern .........
else if (IdString >= 99000 && IdString < 100000)
{
returnString = "00099";
}
else
{
returnString = "00000";
}
return returnString;
}
Instead of doing that I want to have a function which can do the same. I tried it in the following way but couldn’t get it to work. The pseudo code:
private string ShortFunction(string IdString)
{
string returnString = string.Empty;
if (Information.IsNumeric(IdString))
{
int intStrId = Convert.ToInt32(IdString);
int thousandValueOfIdString = HowToExtractFromIdString();//???
if (IdString > thousandValueOfIdString && IdString < thousandValueOfIdString + 1000)
{
returnString = String.Format("????:", thousandValueOfIdString); //eg. "00050";
}
}
return returnString;
}
Can anyone give me an idea?
NOTE:I am updating inherited code from the previous developers. Original code has been modified several times during past 2 years as IdString increased beyond the assumed value. When I traced, I found that this IdString coming from auto incremented bigint column in SQL Server 2008.
Upvotes: 0
Views: 204
Reputation: 66469
Convert it to an int
, perform integer division (which drops the fractional part), and pad the string using the D
numeric format string:
return (Convert.ToInt32(IdString) / 1000).ToString("D5");
Output:
1000 -> 00001
If the number goes higher than 99999, you can either increase the number 5 in D5
to add more leading zeroes, or just leave it as is.
@GrantWinney. Padding always three 0s on left.
In that case, if you want a minimum of three zeroes for padding, I'd split it into two lines of code. I'm using the Math.Max method to provide 5 zeroes on smaller number, but at least 3 zeroes on larger numbers.
var thous = (Convert.ToInt32(IdString) / 1000).ToString();
return thous.PadLeft(Math.Max(5, thous.Length + 3), '0');
Output:
1000 -> 00001
22500 -> 00022
999999 -> 000999
Upvotes: 2
Reputation: 15202
private String ShortFunction(string IdString)
{
Int32 id = Int32.Parse(IdString); // Convert input to a workable numeric value
Int32 outputInt = id / 1000; // Obtain the thundsand value you after
String outputString = outputInt.ToString().PadLeft(5, '0'); // Convert the output number to desired string format
return outputString;
}
.
Notes on proposed method:
Upvotes: 2
Reputation: 142
You could convert IdString
to int
, then get rid of the last 3 digits
something like this
int number = int.Parse(IdString);
int result = (number - (number%1000))/1000;
just number/1000
works too, but I don't like that practice very much, so I avoid it.
if(result >100)
return "00000"; //check the last case
Then, convert it back to String
return result.ToString("D5"); //this gives you the number with 5 digits
Upvotes: 1