Amigos to preciso formatar campos texto HTML para o formato de moeda… alguem sabe ?:
A quem puder ajudar agradeco.
Amigos to preciso formatar campos texto HTML para o formato de moeda… alguem sabe ?:
A quem puder ajudar agradeco.
/***************************************************************
* Rotina utizada para formatar campos monetarios *
***************************************************************/
function Limpar(valor, validos) {
var result = "";
var aux;
for (var i=0; i < valor.length; i++) {
aux = validos.indexOf(valor.substring(i, i+1));
if (aux>=0) {
result += aux;
}
}
return result;
}
/*******************************************************************************
* Formata número tipo moeda usando o evento onKeyDown
* A formatação permite apagar um numero e retornar uma casa decimal.
*******************************************************************************/
function FormataValor(campo,tammax,teclapres,decimal) {
var tecla = teclapres.keyCode;
vr = Limpar(campo.value,"[telefone removido]");
tam = vr.length;
dec=decimal
if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
if (tecla == 8 )
{ tam = tam - 1 ; }
if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
if ( tam <= dec )
{ campo.value = vr ; }
if ( (tam > dec) && (tam <= 5) ){
campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 6) && (tam <= 8) ){
campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ;
}
if ( (tam >= 9) && (tam <= 11) ){
campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 12) && (tam <= 14) ){
campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 15) && (tam <= 17) ){
campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
}
}
use assim: <input type=“text” onkeydown=“FormataValor(this,20,event,2)”">
falows qualquer duvida…
okay… so que o seguinte campos em que eu digito ele formata beleza…
mais eu tb tenho campos que recebem valores atraves de uma funcao… como vou fazer para formatar ??? pq esse campos que receberam valores nao formataram…
essa funcao em que vc me passou funciona… so que ele so formata seu eu digitar … entendeu?
public static String formatNumero(double valor) {
return formatNumero(valor, "###,##0.00");
}
public static String formatNumero(double valor, String formato) {
try{
DecimalFormat myFormatter = new DecimalFormat(formato);
DecimalFormatSymbols dfs = new DecimalFormatSymbols(new Locale("pt","BR"));
dfs.setMonetaryDecimalSeparator('.');
myFormatter.setDecimalFormatSymbols(dfs);
return myFormatter.format(valor);
}catch(Exception e) {
return e.getLocalizedMessage();
}
}
ok;… hum comoeu poderia usar para formatar campo texto HTML. pois uso HTML + JSP
Entao por que vc não utiliza um parser?!
Ficaria mais ou menos o seguinte…
static final DecimalFormat FORMATO = new DecimalFormat("###,###,##0.00");
/* Esta é uma formatação qqer.... coloquei aki só como exemplo...
No caso, modifique-a como quiser para sua aplicação! */
public Object formatar( BigDecimal valor ) {
try {
valor = new BigDecimal( FORMATO.parse(valor).doubleValue() );
} catch (ParseException e) {
// Exception
}
return valor.setScale(2, RoundingMode.HALF_UP);
}
/* Para campos string ficaria: */
public String formataStr( String valor ) {
double dValue = 0d;
if ( valor!= null ) {
dValue = ((Number)valor).doubleValue();
return FORMATO.format(dValue);
}
return null;
}
Teste…!!
Inté…