This is topic What does this bit of code do - What's the %1000 ? in forum Life at TMO Talk.


To visit this topic, use this URL:
http://www.themoononline.com/cgi-bin/Forum/ultimatebb.cgi?ubb=get_topic;f=11;t=000437

Posted by Darryn.R (Member # 1) on :
 
(It's to create an order ID, but they're coming in too long now it's 2010..


int iYYYY = cal.get(java.util.Calendar.YEAR);

int iY = (iYYYY % 1000);

sY = Integer.toString(iY, 10);


Full string:

{

String sOrderID = null;

GregorianCalendar cal = new GregorianCalendar();

String sY = null;

String sDDD = null;

String sHH = null;

String smm = null;

String sSS = null;

int iYYYY = cal.get(java.util.Calendar.YEAR);

int iY = (iYYYY % 1000);

sY = Integer.toString(iY, 10);

int iDDD = cal.get(java.util.Calendar.DAY_OF_YEAR);

sDDD = Integer.toString(iDDD, 10);

sDDD = fitString(sDDD, 3, PAD_LEFT, '0');

int iHH = cal.get(java.util.Calendar.HOUR_OF_DAY);

sHH = Integer.toString(iHH, 10);

sHH = fitString(sHH, 2, PAD_LEFT, '0');

int imm = cal.get(java.util.Calendar.MINUTE);

smm = Integer.toString(imm, 10);

smm = fitString(smm, 2, PAD_LEFT, '0');

int iSS = cal.get(java.util.Calendar.SECOND);

sSS = Integer.toString(iSS, 10);

sSS = fitString(sSS, 2, PAD_LEFT, '0');

sOrderID = sY + sDDD + sHH + smm + sSS;

return sOrderID;

[ 08.01.2010, 07:53: Message edited by: Darryn.R ]
 
Posted by dang65 (Member # 102) on :
 
I don't know Java, but it looks like that's to do with adding a "0" pad for the last ten years (2000-2009).

The % is a "remainder operator", so it's dividing the year by 1000 and returning the remainder - e.g. 9 last year. I've lost track a bit after that, but it's something along those lines, where it would change "9" to "09" because you're only using the two-digit year reference instead of the whole "2009".
 
Posted by dang65 (Member # 102) on :
 
Actually, I can't see where the year number is being padded, so maybe it isn't?

It's creating the OrderID with the first digit, or digits, being the year once you've dumped the thousands.

So the last ten years would have been one digit, and now it's two digits. Which is presumably making your OrderID too long?

The problem was created when the code was first written and didn't pad the year.

If I've got that right.
 
Posted by Darryn.R (Member # 1) on :
 
Bingo - That's it - Thanks Dang.

I couldnb't get my head round the "remainder operator" - The bloody years not padded, so 10 is one digit too long.
 


copyright TMO y2k+

Powered by Infopop Corporation
UBB.classic™ 6.6.1