|
-
Feb 11th, 2010, 05:23 AM
#1
Thread Starter
Fanatic Member
C# beginner Numering question
Hello,
We have a program at work wich runs on C# (I am new to this).
There is a function (seen in the code below) which numbers ID's.
When I choose 3 letters like 'AZM' the numbering works.
AZM001
AZM002
AZM003
etc...
But when I want to use numbers: 001001, 001002, 001003 it doesnt work.
I get:
001001 (this is good)
011002 (this should be 001002)
011003 (this should be 001003)
I hope that you guys can help.
Code:
function GetPaddedPatNo(LastID, padding)
{
var snums = '0123456789';
var sres = '';
var i = 0;
// remove all non-numerical characters
for(i=0; i< LastID.length; i++)
{
if(snums.indexOf(LastID.substr(i,1)) >= 0)
sres += LastID.substr(i,1);
}
var ires = new Number(sres);
if((ires.NaN) || (ires == 0))
ires = 0;
if(ires > 9999)
ires = ires % 10000;
var nextPat = ires + 1;
sres = nextPat + '';
while(sres.length < padding)
sres = '0' + sres;
return(sres);
}
@INST_SHORTCODE + GetPaddedPatNo(LastPfxPatNo(@INST_SHORTCODE), 3);
-
Feb 11th, 2010, 05:28 AM
#2
Re: C# beginner Numering question
Do you want to add 00 the result ?
Please mark you thread resolved using the Thread Tools as shown
-
Feb 11th, 2010, 05:35 AM
#3
Thread Starter
Fanatic Member
Re: C# beginner Numering question
 Originally Posted by danasegarane
Do you want to add 00 the result ?
I don't know if I understand you.
001 is the studycode.
Per study there are multiple participant which incriments by 1 001, 002, etc..
So if a student is added to the study it looks like:
001001,001002,001003,etc
-
Feb 11th, 2010, 06:53 AM
#4
Re: C# beginner Numering question
This worked for me
Code:
function padNumber(number,length) {
var str = '' + number;
while (str.length < length)
str = '0' + str;
return str;
}
function GetPaddedPatNo(number,pad)
{
var mystr=stripAlphaChars(number);
var ires = new Number(mystr);
var newNumber;
if((ires.NaN) || (ires == 0))
ires = 0;
if(ires > 9999)
ires = ires % 10000;
alert(padNumber(mystr,pad));
}
function stripAlphaChars(pstrSource)
{
var m_strOut = new String(pstrSource);
m_strOut = m_strOut.replace(/[^0-9]/g, '');
return m_strOut;
}
call as
return GetPaddedPatNo('4', 3);
Last edited by danasegarane; Feb 11th, 2010 at 07:05 AM.
Please mark you thread resolved using the Thread Tools as shown
-
Feb 12th, 2010, 03:34 AM
#5
Re: C# beginner Numering question
You don't need to write custom code to pad strings.
csharp Code:
var num = 1; var str = num.ToString("000"); MessageBox.Show(str);
csharp Code:
var num = 1; var str = num.ToString().PadLeft(3, '0');; MessageBox.Show(str);
csharp Code:
var num1 = 1; var num2 = 2; var str = string.Format("{0:000}{1:000}", num1, num2); MessageBox.Show(str);
-
Feb 12th, 2010, 04:51 AM
#6
Re: C# beginner Numering question
I think question is in Javascript
Please mark you thread resolved using the Thread Tools as shown
-
Feb 12th, 2010, 06:55 AM
#7
Re: C# beginner Numering question
 Originally Posted by danasegarane
I think question is in Javascript 
Now that I look at the original code, I think maybe you're right. That leads me to ask, why is this question in the C# forum?
-
Feb 12th, 2010, 07:20 AM
#8
Re: C# beginner Numering question
-
Feb 12th, 2010, 07:21 AM
#9
Re: C# beginner Numering question
Please mark you thread resolved using the Thread Tools as shown
-
Feb 23rd, 2010, 09:43 AM
#10
Thread Starter
Fanatic Member
Re: C# beginner Numering question
 Originally Posted by jmcilhinney
Now that I look at the original code, I think maybe you're right. That leads me to ask, why is this question in the C# forum?
Sorry for that.
Normally I use VB, but a coworker asked me to take a look at it.
Here is how is was solved:
Code:
function GetPaddedPatNo(LastID, padding)
{
var snums = '0123456789';
var sres = '';
var i = 0;
// remove all non-numerical characters
for(i=0; i< LastID.length; i++)
{
if(snums.indexOf(LastID.substr(i,1)) >= 0)
sres += LastID.substr(i,1);
}
var ires = new Number(sres);
if((ires.NaN) || (ires == 0))
ires = 0;
if(ires > 999)
ires = ires % 1000;
var nextPat = ires + 1;
sres = nextPat + '';
while(sres.length < padding)
sres = '0' + sres;
return(sres);
}
@INST_SHORTCODE + GetPaddedPatNo(LastPfxPatNo(@INST_SHORTCODE), 3);
Thank you very much for helping guys..
And see u soon at the VB.NET forums ;-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|