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

Formatter un chiffre ("%4f.2") - Java - Programmation

Marsh Posté le 12-02-2008 à 18:14:27    

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

Reply

Marsh Posté le 12-02-2008 à 18:14:27   

Reply

Marsh Posté le 12-02-2008 à 20:48:48    

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, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 13-02-2008 à 08:45:35    

le JDK 1.6

Reply

Marsh Posté le 13-02-2008 à 09:22:46    

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, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 13-02-2008 à 10:04:30    

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
Reply

Marsh Posté le 13-02-2008 à 10:22:11    

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
Reply

Marsh Posté le 13-02-2008 à 10:25:46    


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, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed