The Artima Developer Community
Sponsored Link

Java Answers Forum
Leading Zeros are dropped?

2 replies on 1 page. Most recent reply: Dec 18, 2004 1:13 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Dar Nessar

Posts: 4
Nickname: dardaman
Registered: Oct, 2004

Leading Zeros are dropped? Posted: Dec 16, 2004 8:38 AM
Reply to this message Reply
Advertisement
I have a JavaScript question, and although this is a Java Forum, I'm sorry but I don't know where else to post this.

I have a form where a user enters a 3-digit number. The code takes the number and increments it by 1 as long as the number entered is not 999.

The problem is that if the number entered by the user is, for example, 023, the code increments the number to 24, which is right, but it drops the leading 0. It should increment the number to show 024.

Can someone please tell me how to fix this problem? My code is below:




function btn()
{
var num=document.frm.txt.value


if (num == 999) {
alert("You have reached the maximum number of " + num)
}
else {
num++
alert("The new number is " + num)
}
}


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Leading Zeros are dropped? Posted: Dec 16, 2004 5:58 PM
Reply to this message Reply
<HTML>
<HEAD>
<TITLE></TITLE>
	<script language="JavaScript">
	function btn()
	{
		var num=document.frm.txt.value
 
 
		if (num == 999) {
			alert("You have reached the maximum number of " + num)
		}
		else {
			num++
			document.frm.txt.value = getZeroPaddedValue(num);
		}
	}
 
	function getZeroPaddedValue(num) {
		var paddedValue = num + "";
		if ( num < 0 ) {
			return paddedValue;
		}
 
		var PADDED_LENGTH = 3;
		var zeros_required = PADDED_LENGTH - paddedValue.length ;
		var zeros = "";
		for ( var i = 0 ; i < zeros_required; i++ ) {
			zeros = zeros + "0";
		}
 
		paddedValue = zeros + paddedValue ;
		return paddedValue;
	}
</script>
</HEAD>
 
<BODY>
	<form name="frm">
		<input type="text" name="txt" value="000"/>
		<input type="button" name="b1" value="Increment" onClick="btn()"/>
	</form>
 
</BODY>
</HTML>
 

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Leading Zeros are dropped? Posted: Dec 18, 2004 1:13 AM
Reply to this message Reply
Sure would be nice if JavaScript had something like sprintf() (or the Python % string operator). Maybe it does have something like that; you might asking some real JavaScript gurus in the comp.lang.javascript newsgroup or on one of the many JavaScript web sites (see Google). Anyway, this little function will do the specific thing you are asking about:
function PadInt(i,width,pad)
{
   if( i < 0 || (i + "").length >= width )
      return i;
   return PadInt(pad + i,width,pad);
}
 
// Here's how you might call it:
zeroFourFour = PadInt( 44, 3, "0" );

Flat View: This topic has 2 replies on 1 page
Topic: Problem with dropdown Previous Topic   Next Topic Topic: How to repaint a panel

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use