Formatação de números

3 respostas
M

Olá pessoal uso código abaixo para formatar números que pego do formulário e armazeno no banco

String str = tfDescontoAdicional.getText().replaceAll("\.", ","); str = str.substring(0, str.lastIndexOf(",")) + "." + str.substring(str.lastIndexOf(",")+1, str.length());

O problema é quando tenho números maior que 999,99 e números q não tem vírgula. Ele dá o seguinte erro:

Número sem vírgula

Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(Unknown Source) at br.com.sstintas.view.VendaView$Foco3Listener.focusLost(VendaView.java:1264) at java.awt.AWTEventMulticaster.focusLost(Unknown Source) at java.awt.Component.processFocusEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

Número maior que 999,99

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1,505.20" at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Float.parseFloat(Unknown Source) at br.com.sstintas.view.VendaView$1.actionPerformed(VendaView.java:451) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

Gostaria de sugestões!!

3 Respostas

V

java.math.BigDecimal

T
String str = tfDescontoAdicional.getText().replaceAll("\.", ",");
 			str = str.substring(0, str.lastIndexOf(",")) + "." + str.substring(str.lastIndexOf(",")+1, str.length());

Pelo que você nos diz, você quer converter “12.345,67” para “12345.67”.
Pra começar, se você puder converter isso diretamente para um double ou BigDecimal seria melhor, em vez de ficar montando o SQL na mão.
Supondo que você precise montar o SQL na mão, em vez de usar PreparedStatement:

String str = tfDescontoAdicional.getText().replaceAll ("\.", "").replaceAll (",", ".");
M

Obrigada pela dica, era isso mesmo!!

Criado 9 de agosto de 2006
Ultima resposta 9 de ago. de 2006
Respostas 3
Participantes 3