Problema com a formatação de um double

2 respostas
D

Boa noite pessoal,

Estou com um problema… é o seguinte…

double roundTwoDecimals(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
    }
decim = pj;
        roundTwoDecimals(decim);
        jTextField5.setText(String.valueOf(decim));

Erro:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "547,64" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224) at java.lang.Double.valueOf(Double.java:475) at my.equivPJ.CLTxPJ.roundTwoDecimals(CLTxPJ.java:297) at my.equivPJ.CLTxPJ.jButton1ActionPerformed(CLTxPJ.java:364) at my.equivPJ.CLTxPJ.access$000(CLTxPJ.java:9) at my.equivPJ.CLTxPJ$1.actionPerformed(CLTxPJ.java:63) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6038) at javax.swing.JComponent.processMouseEvent(JComponent.java:3260) at java.awt.Component.processEvent(Component.java:5803) at java.awt.Container.processEvent(Container.java:2058) at java.awt.Component.dispatchEventImpl(Component.java:4410) at java.awt.Container.dispatchEventImpl(Container.java:2116) at java.awt.Component.dispatchEvent(Component.java:4240) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) at java.awt.Container.dispatchEventImpl(Container.java:2102) at java.awt.Window.dispatchEventImpl(Window.java:2429) at java.awt.Component.dispatchEvent(Component.java:4240) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

Alguém sabe o que está acontecendo?
Eu tentei aplicar a outros números e jtextfields e deu certo… só nesse que não está dando…
:?

Obrigada desde já,
Dina

2 Respostas

L

Tem certeza que vc quer perder precisão???

Não sei se tem um jeito mais elegante mas:

DecimalFormat twoDForm = new DecimalFormat("#.##");
		return twoDForm.parse(twoDForm.format(a)).doubleValue();

ou

return new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP)
				.doubleValue();

Mas, se for para formatar uma entrada do usuario, porque vc nao usa algum componente que ja formata para vc!? tipo

http://java.sun.com/developer/JDCTechTips/2004/tt0504.html

D

:)Obrigada pela resposta…

Dina

Criado 28 de abril de 2009
Ultima resposta 11 de mai. de 2009
Respostas 2
Participantes 2