Ir para conteúdo
Fórum Script Brasil
  • 0

Formatar String, Casas Decimais,


kbalera

Pergunta

Ai galera do precisando de fazer o seguinte

eu tenho uma variavel

tipo

var a = 1;

so q eu quero fazer ela ter o valor de

0001 -> tipo com quatro casas decimais

tipo formatar uma variavel pra ter4 casas decimais

em php eu consigo fazer mas to precisando fazer em javascript

sera que alguém pode me ajudar?

JUNINHO

Link para o comentário
Compartilhar em outros sites

1 resposta a esta questão

Posts Recomendados

  • 0

Dá uma olhada nesse código

function FormataVALOR(nome, cpVir, perc){
	VALOR = getValue(nome);
	if(VALOR.substring(VALOR.length-2, VALOR.length) == " %" || VALOR.substring(VALOR.length-2, VALOR.length) == " C" || VALOR.substring(VALOR.length-2, VALOR.length) == " D"){
  VALOR = VALOR.substring(0, VALOR.length-2);
	}
	qtd = VALOR.length;
	if(qtd > 0){
  for(a=qtd; a>0; a--){
  	pv = 0;
  	if(VALOR.substring(a-1,a) == '.' || VALOR.substring(a-1,a) == ','){
    pv = a;
    a = 0;
  	}
  }
  if(pv == 0){
  	if(cpVir == 0) zeros = '';
  	else zeros = ',';
  	for(i=0; i < cpVir; i++){ zeros += '0' }
  	if(VALOR.length > 3)VALOR = colocaPonto(VALOR.length, VALOR);
  	VALOR = VALOR+zeros;
  } else {
  	depoisVir = VALOR.substring(pv, qtd);//aqui mexe-se com as casas após a vírgula.
  	zeros = '';
  	if(depoisVir.length < cpVir){
    for(b=depoisVir.length; b < cpVir; b++) zeros += '0';
  	} else if(depoisVir.length > cpVir) erroValue = ('Você excedeu as casas após a vírgula permitido');
  	depoisVir = depoisVir+zeros;
  	// aqui mexe-se com as casas antes da vírgula.
  	av = VALOR.substring(0,pv-1);
  	av = Limp(av);
  	ant = av.length;
  	variavel = av;
  	for(j=0; j < ant; j++){
    if(av.substring(j,j+1)=='0')variavel = variavel - av.substring(0,1);
    else j = ant;
  	}
  	av = ''+variavel;
  	ant = av.length;
  	if(ant == 0){
    antesVir = '0';
  	} else {
    if(ant < 4){
    	antesVir = av;
    } else {
    	antesVir = colocaPonto(ant, av);
    }
  	}
  	VALOR = antesVir+','+depoisVir;
  }
  if(VALOR.substring(VALOR.length-1, VALOR.length) != "%"){
  	if(perc) VALOR = VALOR+" %";
  }
  setValue(nome, VALOR);
	}
}

function tiraPonto(c) {
	while((cx=c.indexOf("."))!=-1) { c = c.substring(0,cx)+c.substring(cx+1); }
	return(c);
}

function colocaPonto(ant, av){
	//av = av;
	mod = ant%3;
	if (ant < 7){
  if(mod==0)a = av.substring(0,3)+'.'+av.substring(3,6);
  if(mod==1)a = av.substring(0,1)+'.'+av.substring(1,4);
  if(mod==2)a = av.substring(0,2)+'.'+av.substring(2,5);
	} else {
  if(ant < 10){
  	if(mod==0)a = av.substring(0,3)+'.'+av.substring(3,6)+'.'+av.substring(6,9);
  	if(mod==2)a = av.substring(0,2)+'.'+av.substring(2,5)+'.'+av.substring(5,8);
  	if(mod==1)a = av.substring(0,1)+'.'+av.substring(1,4)+'.'+av.substring(4,7);
  } else {
  	if(ant < 13){
    if(mod==0)a = av.substring(0,3)+'.'+av.substring(3,6)+'.'+av.substring(6,9)+'.'+av.substring(9,12);
    if(mod==2)a = av.substring(0,2)+'.'+av.substring(2,5)+'.'+av.substring(5,8)+'.'+av.substring(8,11);
    if(mod==1)a = av.substring(0,1)+'.'+av.substring(1,4)+'.'+av.substring(4,7)+'.'+av.substring(7,10);
  	}else{
    if(mod==0)a = av.substring(0,3)+'.'+av.substring(3,6)+'.'+av.substring(6,9)+'.'+av.substring(9,12)+'.'+av.substring(12,15);
    if(mod==2)a = av.substring(0,2)+'.'+av.substring(2,5)+'.'+av.substring(5,8)+'.'+av.substring(8,11)+'.'+av.substring(11,14);
    if(mod==1)a = av.substring(0,1)+'.'+av.substring(1,4)+'.'+av.substring(4,7)+'.'+av.substring(7,10)+'.'+av.substring(10,13);
  	}
  }
	}
	return a;
}

function getValue(nome){
	var obj = eval("document.forms[0]."+nome+".value");
	return obj;
}
function setValue(nome, valor){
	obj = eval("document.forms[0]."+nome);
	obj.value = valor;
}

Esse código eu fiz há algum tempo atrás, não me lembro se tá funcionando, dá uma olhda e depois me responde se funcionou ou não

Abraço

Crystian

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...