IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants Java Discussion :

Problème avec DecimalFormat


Sujet :

Composants Java

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    149
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 149
    Points : 95
    Points
    95
    Par défaut Problème avec DecimalFormat
    Bonsoir,
    J'utilise DecimalFormat dans le but de n'avoir que 2 chiffres après la virgule et je rencontre un souci. Voici le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public void actionPerformed(ActionEvent e) {
    		DecimalFormat df = new DecimalFormat();
    		df.setMaximumFractionDigits(2);
    		//df.setGroupingSize(3);
    		Object source = e.getSource();
    		if (source == jTextFieldPU) {
    		     String pu = ""+ ((new BigDecimal(jTextFieldQuantite.getText()))
    			.multiply((new BigDecimal(jTextFieldPU.getText()))));
    			jTextFieldHT.setText(df.format(Double.parseDouble(pu)));
    			System.out.println(pu);
    		}
    		if (source == jComboTaux) {
    			
    			String pTTC = ""+ (((new BigDecimal(jComboTaux.getSelectedItem().toString()))
    							.multiply((new BigDecimal(jTextFieldHT.getText())).divide(new BigDecimal("100")))).
    							add(new BigDecimal(jTextFieldHT.getText())));
    			jTextFieldTTC.setText(df.format(Double.parseDouble(pTTC)));
    			System.out.println(pTTC);
    		}
    	}
    et j'obtiens NumberFormatException.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
     
    Exception occurred during event dispatching:
    java.lang.NumberFormatException
    	at java.math.BigDecimal.<init>(Unknown Source)
    	at java.math.BigDecimal.<init>(Unknown Source)
    	at fr.cesci.stock.Purchase.actionPerformed(Purchase.java:626)
    	at javax.swing.JComboBox.fireActionEvent(Unknown Source)
    	at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    	at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    	at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
    	at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at javax.swing.plaf.basic.BasicComboPopup$1.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.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.Dialog$1.run(Unknown Source)
    	at java.awt.Dialog$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.awt.Dialog.show(Unknown Source)
    	at java.awt.Component.show(Unknown Source)
    	at java.awt.Component.setVisible(Unknown Source)
    	at java.awt.Window.setVisible(Unknown Source)
    	at java.awt.Dialog.setVisible(Unknown Source)
    	at fr.cesci.stock.Gestion_Stock$8.actionPerformed(Gestion_Stock.java:323)
    	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.AbstractButton.doClick(Unknown Source)
    	at javax.swing.AbstractButton.doClick(Unknown Source)
    	at javax.swing.plaf.basic.BasicMenuItemUI$Actions.actionPerformed(Unknown Sourc
    e)
    	at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    	at javax.swing.JComponent.processKeyBinding(Unknown Source)
    	at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(Unknown Source)
    	at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(Unknown Source)
    	at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(Unknown Source)
    	at javax.swing.JMenuBar.processKeyBinding(Unknown Source)
    	at javax.swing.KeyboardManager.fireBinding(Unknown Source)
    	at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
    	at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
    	at javax.swing.JComponent.processKeyBindings(Unknown Source)
    	at javax.swing.JComponent.processKeyEvent(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.dispatchKeyEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(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.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(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)
    J'ai mis certaine partie du code en rouge car je crois que le problème vient de là mais je n'arrive pas à le régler.
    Aidez-moi SVP.

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    149
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 149
    Points : 95
    Points
    95
    Par défaut
    Bonjour,

    J'ai réussi à régler le prob. en fait cela venait de l'embrouille due au format décimal à la française (la virgule comme séparateur de décimal). J'ai donc utilisé NumberFormat à la place de DecimalFormat, ce qui m'a permis de définir l'attribut "Locale" en "US" et ça marche.

    Par contre, je n'ai pas cherché à savoir si "DecimalFormat" permettait de définir l'attribut Locale.

    Merci à tous

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème avec DecimalFormat
    Par bedomon dans le forum Débuter avec Java
    Réponses: 8
    Dernier message: 29/08/2013, 16h44
  2. Problème avec DecimalFormat
    Par stof dans le forum Collection et Stream
    Réponses: 5
    Dernier message: 12/06/2008, 16h09
  3. Problème avec DecimalFormat
    Par g0ldenrno dans le forum Langage
    Réponses: 10
    Dernier message: 20/06/2006, 18h45
  4. Problème avec la mémoire virtuelle
    Par Anonymous dans le forum CORBA
    Réponses: 13
    Dernier message: 16/04/2002, 16h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo