Dush
Dush

Reputation: 1365

How to get least and most thousand values of a numeric string in C#?

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

Answers (3)

Grant Winney
Grant Winney

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

Trav L
Trav L

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:

  • You may require to expand for input validation on edge cases (eg/ what happen with IdString contains a negative value)
  • I use Int32 which handles up to 2,147,483,647 only.
  • The output will exceed 5 character string value if input value contains > 100,000,000.

Upvotes: 2

David Zhou
David Zhou

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

Related Questions