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);
Re: C# beginner Numering question
Do you want to add 00 the result ?
Re: C# beginner Numering question
Quote:
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
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);
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);
Re: C# beginner Numering question
I think question is in Javascript :)
Re: C# beginner Numering question
Quote:
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?
Re: C# beginner Numering question
Re: C# beginner Numering question
Re: C# beginner Numering question
Quote:
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 ;-)