locked
Truncating numbers to remain it two numbers (Not decimal) RRS feed

  • Question

  • User1227157679 posted
    Hi everyone, Not to say I am new to ASP.Net/VB.Net but I'm just not good at it. So I'm going to say what I'm missing out while coding. I have an id called "AM01". I separated them by truncating the numbers out and increase it by 1 each time I want to add a new record. Therefore before I add the record, I would have to combine them back. I managed to truncate them and combine them back. However, the results turn out to be "AM2" instead of "AM02". So, is there a way where I could make the numbers as "02" instead of just "2"? Thanks.
    Thursday, March 19, 2009 6:00 AM

Answers

  • User-319574463 posted

     To allow posting of code, please do the following:

    • Click on Edit Profile
    • Select the Site Options tab
    • Change Content Editor to Enhanced
    • Click Save
    • When your next post (using IE or Firefox), you will have an extra toolbar and at the far right of it is a button for code entry.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 21, 2009 5:49 AM
  • User-319574463 posted

     Here is some code that gets the next number. This is done as a unit test in C#

        [Test]
        public void GetNetKey()
        {
          var test = "AM01";
          Assert.AreEqual(4, test.Length);    //// method requires a 4-character input
          var prefix = test.Substring(0, 2);  //// get prefix
          var suffix = test.Substring(2, 2);  //// get existing numeric part
          var number = 1 + Convert.ToInt32(suffix); //// get next number
          var result = prefix + number.ToString("00");  //// note the "00" format
          Assert.AreEqual("AM02", result);    //// assert result is as expected
        }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 21, 2009 6:01 AM

All replies

  • User-990694832 posted

    A person would have to add an if statement to see if the number is less than 10.

    In addition, use system.text.stringbuilder to build the id.

    Also, what is the id ? a control or a database key ?? 

     

    Hope this helps

    DK 

    Thursday, March 19, 2009 9:22 AM
  • User1227157679 posted
    It's a primary key in one of my tables. I searched through google and almost everywhere. There's no syntax in doing so. So, I end up making an if statement right before i check here again. And it fits what u said lol. It's working. Btw, thanks for replying, DK. :)
    Thursday, March 19, 2009 9:40 AM
  • User1227157679 posted
    Weird. My sentences are all sticking together. I can't post the code here :S
    Thursday, March 19, 2009 9:40 AM
  • User-319574463 posted

     To allow posting of code, please do the following:

    • Click on Edit Profile
    • Select the Site Options tab
    • Change Content Editor to Enhanced
    • Click Save
    • When your next post (using IE or Firefox), you will have an extra toolbar and at the far right of it is a button for code entry.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 21, 2009 5:49 AM
  • User-319574463 posted

     Here is some code that gets the next number. This is done as a unit test in C#

        [Test]
        public void GetNetKey()
        {
          var test = "AM01";
          Assert.AreEqual(4, test.Length);    //// method requires a 4-character input
          var prefix = test.Substring(0, 2);  //// get prefix
          var suffix = test.Substring(2, 2);  //// get existing numeric part
          var number = 1 + Convert.ToInt32(suffix); //// get next number
          var result = prefix + number.ToString("00");  //// note the "00" format
          Assert.AreEqual("AM02", result);    //// assert result is as expected
        }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 21, 2009 6:01 AM
  • User1227157679 posted

    Thanks :)

    This is hardcoded for 10 numbers.

    Dim intTruncAt

    intTruncAt = 2

    If Len(id) > intTruncAt Then

    id = Right(id, intTruncAt)

    id += 1

    If id < 10 Then

    id = "AM0" + id

    Else

    .... <something>

    End If

    End If

    Saturday, March 21, 2009 10:00 AM