Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
5240 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Formatter un chiffre ("%4f.2")

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Formatter un chiffre ("%4f.2")

n°1685490
sedrilo
Posté le 12-02-2008 à 18:14:27  profilanswer
 

Bonjour,  
 
J'ai un BigDecimal, par exemple    BigDecimal bd = new BigDecimal("123.456789" );
 
Et je récupère d'un fichier externe une String : "%4f.2".
 
Le but est de récupérer des objets BigDecimal avec le format "%4f.2".
 
Pour prendre l'exemple ci-dessus, je veux récupérer 0123.45
(J'ai cru comprendre que %4f.2 signifie un float avec 4 chiffres à gauche, 2 chiffres à droite de la virgule)
 
Je ne trouve pas les méthodes JAVA pour y arriver.
J'ai essayé avec la classe Formatter, mais je n'ai pas réussi.
 
Pouvez-vous m'aider?
Merci

mood
Publicité
Posté le 12-02-2008 à 18:14:27  profilanswer
 

n°1685546
masklinn
í dag viðrar vel til loftárása
Posté le 12-02-2008 à 20:48:48  profilanswer
 

quelle version de java?

 

edit: et c'est 4.2f pas 4f.2


Message édité par masklinn le 12-02-2008 à 20:49:25

---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
n°1685634
sedrilo
Posté le 13-02-2008 à 08:45:35  profilanswer
 

le JDK 1.6

n°1685646
masklinn
í dag viðrar vel til loftárása
Posté le 13-02-2008 à 09:22:46  profilanswer
 

Alors tu as String.format qui est plus simple à utiliser que Formatter.
 
Et accessoirement, je ne suis pas certain que BigDecimal puisse être formatté avec %f, %f sert à formatter des flottants après tout...


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?
n°1685679
sedrilo
Posté le 13-02-2008 à 10:04:30  profilanswer
 

System.out.println(String.format("%4.2f dB", new BigDecimal("123.456789" )));
me renvoie 123.46 dB
 
edit : En fait un String en sortie me convient aussi
C'est ce que je voulais! Merci!!!

Message cité 1 fois
Message édité par sedrilo le 13-02-2008 à 10:10:11
n°1685690
Taz
bisounours-codeur
Posté le 13-02-2008 à 10:22:11  profilanswer
 

sedrilo a écrit :

System.out.println(String.format("%4.2f dB", new BigDecimal("123.456789" )));
me renvoie 123.46 dB

 

edit : En fait un String en sortie me convient aussi
C'est ce que je voulais! Merci!!!


printf

Message cité 1 fois
Message édité par Taz le 13-02-2008 à 10:22:17
n°1685692
masklinn
í dag viðrar vel til loftárása
Posté le 13-02-2008 à 10:25:46  profilanswer
 


Si c'est juste pour faire une sortie, effectivement :jap:


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Formatter un chiffre ("%4f.2")

 

Sujets relatifs
[VB] Déterminer une colonne par une lettre et non un chiffrechiffre aléatoire
Aide programme Jeu : Devinez le bon chiffre[EXCEL2007] Problèmes de format (chiffre deviennent dates ; graphes)
[PYTHON] extraire un chiffre au format US 123,456,789Tri de réponses chiffre/caractère [resolu]
Formatter une balise <input type='txt'>Composant identifié grâce à un nom et un chiffre[résolu]
[Excel]ajouter le chiffre d'une autre cellule sur cette meme cellule ?Boucle et tres grand chiffre ?
Plus de sujets relatifs à : Formatter un chiffre ("%4f.2")


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)