473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Zero Up Front function

This function takes as an argument a positive integer and returns a
two character string representing the last two digits.

<script type="text/javascript"><!--

function zeroUpFront(nDigits){
temp = nDigits;

if (temp>=100){
temp = temp%100;
}

if (temp<10){
temp = "" + temp;
temp = "0" + temp;
}
return temp;
}

document.write("If you need a two character representation (i.e. 07)
of what may possibly be a single digit (i.e. 7), use the zeroUpFront()
function that tests and returns a possibly amended argument.")
document.write("<p>zeroUpFront(7) returns " + zeroUpFront(7))

document.write("<p>zeroUpFront(11) returns " + zeroUpFront(11))

function convertRep(number){
document.write("<p>zeroUpFront(" +number+ ") returns " +
zeroUpFront(number))
}

convertRep(5445)

//--></script>

http://www.eastontario.com/promo/zeroUpFront.htm
Jul 20 '05 #1
5 1996
On 5 Mar 2004 11:32:29 -0800, Gord <no*@eastontario.com> wrote:
This function takes as an argument a positive integer and returns a
two character string representing the last two digits.

<script type="text/javascript"><!--
You don't need to hide script data anymore. Lose the SGML comment
delimiters.
function zeroUpFront(nDigits){
temp = nDigits;
This should be a local variable. Use the var keyword.
if (temp>=100){
temp = temp%100;
}

if (temp<10){
temp = "" + temp;
temp = "0" + temp;
}
return temp;
}


A more general way to write this is:

// Pads a value to a specified length
//
// v - A string or number that represents the value to be padded
// n - The required length of the output string
// c - The character used to pad the value.
// Must be a string (defaults to '0')
//
function pad( v, n, c ) {
var t = String( v );
c = c || '0';
while( t.length < n ) t = c + t;
return t;
}

pad( 7, 2 ) returns '07'

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
no*@eastontario.com (Gord) wrote:
This function takes as an argument a positive integer and returns a
two character string representing the last two digits.

<script type="text/javascript"><!--

function zeroUpFront(nDigits){
temp = nDigits;

if (temp>=100){
temp = temp%100;
}

if (temp<10){
temp = "" + temp;
temp = "0" + temp; The result of appending temp to "" and them appending temp to "0" is
exactly the same as only appending temp to "0".
}
return temp;
}

document.write("If you need a two character representation (i.e. 07)
of what may possibly be a single digit (i.e. 7), use the zeroUpFront()
function that tests and returns a possibly amended argument.")
document.write("<p>zeroUpFront(7) returns " + zeroUpFront(7))

document.write("<p>zeroUpFront(11) returns " + zeroUpFront(11))

function convertRep(number){
document.write("<p>zeroUpFront(" +number+ ") returns " +
zeroUpFront(number))
}

convertRep(5445)

//--></script>

http://www.eastontario.com/promo/zeroUpFront.htm


Posting things like this are next to useless IMO. It's one thing to
post a library or a complex function, but what's the point behind such
a simple function of highly specific purpose that can be written
better in 2 lines?

function zeroUpFront(n)
{
n = "00" + n;
return n.substring(n.length-2);
}

BTW, your function doesn't do what you says it does. For inputs 11
through 99 it returns a number, not a string.

Regards,
Steve
Jul 20 '05 #3

Steve van Dongen Wrote:
nov (AT) eastontario (DOT) com (Gord) wrote:
exactly the same as only appending temp to "0".
Posting things like this are next to useless IMO. It's one thing to
post a library or a complex function, but what's the point behind such
a simple function of highly specific purpose that can be written
better in 2 lines?

function zeroUpFront(n)
{
n = "00" + n;
return n.substring(n.length-2);
}

BTW, your function doesn't do what you says it does. For inputs 11
through 99 it returns a number, not a string.

Regards,
Steve


You're partially right, returned is a number if not a single digit.

I think your function is quite different from mine, though recently it
occurred to me that "terser" would have been

function zeroUpFront(nDigits){
temp = nDigits
temp = temp>=100? temp%100:temp
temp = temp<10? ("0" + temp): ("" + temp)
return temp;
}

as used in:
http://eastontario.com/handStories/entry.htm
If you click on one the linked "Deal" links on that page, you'll end up
on another page perhaps of interest, more complex a creation than the
little function above.

I was once very impressed with the simple function suggested on a group
incorporated in the page at:
http://eastontario.com/handStories/handTEMPLATE.htm
which is
var url =
location.href.substring(location.href.length-52,location.href.length);

location.href
is basically it, perhaps an identity more than a function, or a method

Sometimes to just add one more function in to all the rest causes a
chain reaction.

It seemed that the zeroUpFront function was one that perhaps was long
overdue posting, even if so simple.
It wasn't a brainchild, but it seemed it would have been helpful to
have seen it posted somewhere long ago, underlining also the deviance
of javascript from necessary 'casting'.
:cool:
--
eastontario
------------------------------------------------------------------------
eastontario's Profile: http://www.highdots.com/forums/m216
View this thread: http://www.highdots.com/forums/t28612

Apr 19 '06 #4
eastontario <ea****************@no-mx.forums.yourdomain.com.au> writes:
Steve van Dongen Wrote:
function zeroUpFront(n)
{
n = "00" + n;
return n.substring(n.length-2);
}


(a single "0" is even sufficient)
I think your function is quite different from mine, though recently it
occurred to me that "terser" would have been

function zeroUpFront(nDigits){
temp = nDigits
temp = temp>=100? temp%100:temp
temp = temp<10? ("0" + temp): ("" + temp)
return temp;
}
The two methods agree on all parameters that are non-negative integer
numbers less than ~10^20.
I was once very impressed with the simple function suggested on a group
incorporated in the page at:
http://eastontario.com/handStories/handTEMPLATE.htm
which is
var url =
location.href.substring(location.href.length-52,location.href.length);
the second parameter to the substring method is not needed.
location.href is basically it, perhaps an identity more than a
function, or a method
location.href is a string containing the URL of the page in the
current window. I don't know what you mean by "identitiy".
Sometimes to just add one more function in to all the rest causes a
chain reaction.
That sounds like a collision of names, either among the function
declarations themselves, or among global variables used by the
functions.
It seemed that the zeroUpFront function was one that perhaps was long
overdue posting, even if so simple.


There are many versions of it, depending on what it's used for, like
the LZ function here: <URL:http://www.merlyn.demon.co.uk/js-demos.htm#ShFn>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 19 '06 #5
JRS: In article <ea****************@no-mx.forums.yourdomain.com.au>,
dated Wed, 19 Apr 2006 00:48:32 remote, seen in
news:comp.lang.javascript, eastontario <eastontario.26hz1z@no-
mx.forums.yourdomain.com.au> posted :
function zeroUpFront(nDigits){
temp = nDigits
temp = temp>=100? temp%100:temp
temp = temp<10? ("0" + temp): ("" + temp)
return temp;
}
ISTM unwise, in a general function, to remove higher digits, since they
may indicate an error that needs to be fixed.

I see no need for temp>=100? there; % can be applied to numbers in
[0..100) just as well.

The result for an argument of 124.4 may be unexpected.

In article <wt**********@hotpop.com>, dated Wed, 19 Apr 2006 08:21:13
remote, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
<lr*@hotpop.com> posted :
There are many versions of it, depending on what it's used for, like
the LZ function here: <URL:http://www.merlyn.demon.co.uk/js-demos.htm#ShFn>


True; but IMHO <URL:http://www.merlyn.demon.co.uk/js-maths.htm#LZ> &
<URL:http://www.merlyn.demon.co.uk/js-nclds.htm#Inc1> are a better
references for that.

If X is guaranteed to be a non-negative number, then

function LdgZero(X) { return "0".substring(X>=10) + X }

should do (not tested for efficiency).

Perhaps there should be a <FAQENTRY> for Leading Zero.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steve | last post by:
Is there a way in PHP to zero fill an integer to a specified length? For instance, if I have a two digit number, and I want to zero fill it to four digits, is there a PHP function I can use? Or do...
4
by: Randall Parker | last post by:
I am designing a database schema. It happens to be in MySQL but I'm trying to keep it portable to other databases should the project grow. Anyway, suppose you have VARCHAR fields and will be...
3
by: keali | last post by:
for example: A1 = 123 A1=123000 <- final result A1 =1234 A1=123400<- final result A1=1 A1=100000<-final result
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
6
by: Patrick | last post by:
Hello All, Kind of new to PHP. I have an html form where a person inputs a date via a drop down select. How do I make php put a zero in front of the numbers 1-9 ie. 01 02 03 when writing to the...
15
by: angellian | last post by:
Sorry to raise a stupid question but I tried many methods which did work. how can I conserve the initial zero when I try to convert STR(06) into string in SQL statment? It always gives me 6...
8
by: Andrew Poulos | last post by:
In my limited testing with FF 2, IE 6 and Opera 9 when I divided a positive integer, that is less than 100, by 100 I get a leading zero in front of the decimal point. For example 80/100 gives...
2
by: Sune | last post by:
How do I allways get day with two digits, not var dd = d.getDate(); if (dd <10) dd = '0' + dd;
1
by: Monte Cristo | last post by:
Hi all, I'm having a problem with results being returned from a data source, it's returning the right value but without the zero's "0" in front. I've searched through google for "padding zero's in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.