Como faço para formatar numero no java?

1 resposta
G
NumberFormat nf = NumberFormat.getInstance();

String mensagem = "carro: " 
                + cmbCarros.getSelectedItem().toString()
                + "\nTipo de cor: " + msgcor
                + "\nItens: " +itens 
                + "\nTotal de acrecismo: " +acrescimocor nf.getCurrency();
                +"\nValor Total: " +valortotal nf.getCurrency()
txtmensagem.setText(mensagem.toLowerCase());

1 Resposta

peczenyj

https://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html

em especial este tutorial

http://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html

currencyFormatter.format(valor)

vai retornar o valor formatado. vamos preparar este objeto

Currency currentCurrency = Currency.getInstance(currentLocale);

NumberFormat currencyFormatter =

NumberFormat.getCurrencyInstance(currentLocale);

e qual a currentLocale ? vc pode definir o BR

Locale currentLocale = new Locale(“pt”, “BR”);

Criado 29 de outubro de 2016
Ultima resposta 29 de out. de 2016
Respostas 1
Participantes 2