Jump to content
david fagundes

Zero à esquerda em um contador

Recommended Posts

david fagundes

ola pessoal sou novo no java escript dai eu tenho um contador que fica contando mais eu precisava que ele ficasse com pelo menos 10 digitos a esquerda enquanto ele esta contando 

 

o script e o seguinte


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">

var count=new Number();
var count=0;

function start(){
	
	
	if((count + 1) >= 0){
		count=count + 1;
		tempo.innerText=count;
		setTimeout('start();',1000);
	
		
		}
//window.onload=start;
	
	
	
	}

</script>

</head>

<body onload="start();">
<div id="tempo"></div>
</body>
</html>

 
 

gostaria que quando ele contace ficase 

assim 

000001

em vez 

1

 

Share this post


Link to post
Share on other sites
ribeiro55

Olá david.

O que tu procuras é uma função de "padding". Algo como:

function zeroEsquerda(numero, comprimento) { 
	numero = numero.toString(); 
  	while (numero.length < comprimento)        
		numero = "0" + numero;    
  	return numero;
}

Na prática, testa o comprimento do teu número como string. Enquanto for inferior ao comprimento desejado, acrescenta zeros atrás, até chegar ao comprimento certo.

O teu exemplo ficaria:
 

function zeroEsquerda(numero, comprimento) {
	numero = numero.toString();
	while (numero.length < comprimento)
        numero = "0" + numero;
    return numero;
}

var count=new Number();
var count=0;

function start(){
	
	
	if((count + 1) >= 0){
		count=count + 1;
		tempo.innerText=zeroEsquerda(count,6);
		setTimeout('start();',1000);
	
		
		}
//window.onload=start;
	
	
	
	}

Que já agora, se podia simplificar assim:

function zeroEsquerda(numero, comprimento) {
	numero = numero.toString();
	while (numero.length < comprimento)
        numero = "0" + numero;
    return numero;
}

var count=0;

function start(){	
	tempo.innerText=zeroEsquerda(count,6);
	count++;
	setTimeout(start,1000);
}

 


Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Share this post


Link to post
Share on other sites
david fagundes

ribeiro55 muito obrigado ajudou muito tinha semanas quebrando a cabeça com isso.

dai resolvi pedir ajuda obrigado deu certo 

Edited by david fagundes

Share this post


Link to post
Share on other sites
HappyHippyHippo

eu costumo fazer isto:

/// \brief String left padding
/// Function used to pad a string on the left side with a required padding character for an specific amount of characters
///
/// \param padString The character that will be used as padding
/// \param length Required length of the string
/// \return The padded string
String.prototype.lpad = function(padString, length) {
	var str = this;
    while (str.length < length)
        str = padString + str;
    return str;
}

/// \brief String right padding
/// Function used to pad a string on the right side with a required padding character for an specific amount of characters
///
/// \param padString The character that will be used as padding
/// \param length Required length of the string
/// \return The padded string
String.prototype.rpad = function(padString, length)
{
    var str = this;
    while (str.length < length)
        str = str + padString;
    return str;
}

 


IRC : sim, é algo que ainda existe >> #p@p

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.