1

I'm just starting out using Java. And I came across this issue :

This is my code :

import java.util.Scanner;
public class FormatageValeurNumerique {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Entrez une valeur reelle :");
        Scanner in = new Scanner(System.in);
        float x = in.nextFloat();
//      System.out.printf("La valeur reelle est, apres formatage : %.5+,f", x);
//      Gives exception ? Why ?
//      System.out.printf("La valeur reelle est, apres formatage : %+.5,f", x);
//      Gives exception ? Why ?
        System.out.printf("La valeur reelle est, apres formatage : %+,.5f", x);
    }
}

Outputs :

Entrez une valeur reelle : 315136.23 La valeur reelle est, apres formatage : +315,136.21875

And this is what is expected :

Formatage de valeurs numériques

Écrivez un programme qui demande à l’usager une valeur réelle et qui l’affiche à l’écran en s’assurant d’avoir au moins 5 chiffres après la virgule, en forçant l’apparition du signe et en incluant le groupement de chiffres.

L’affichage obtenu doit être semblable au suivant:

Entrez une valeur réelle : 315136.23 La valeur réelle est, après formatage : +315,136.23000

The numbers do not match... any ideas why ?

Also if someone can explain why I'm getting exceptions depending on how I write out the flags ? Is there a priority to respect ?

hayonj
  • 1,429
  • 3
  • 21
  • 28
  • That Worked ! Thank you ... Write it up as an answer and i'll upvote you :) ... Also in your answer if you can explain the differences between using float vs double it would be appreciated ! Please give a few examples of float vs doubles vs BigDecimal – hayonj May 12 '13 at 19:36
  • Done and you're welcome. – Hovercraft Full Of Eels May 12 '13 at 19:40

1 Answers1

1

Don't use float except in very specific circumstances. This has nothing to do with printf and all to do with the fact that you are loosing precision when using float as your floating point type. Use double which has double the precision of float or BigDecimal which has a precision that you define.

I think that the only time I use floats is when calling Java core class methods that expect a float parameter such as some Swing constants and when setting a Swing Font's size. The additional overhead with use of double is minimal. Also you should not use float nor double for financial calculations as these require extreme precision that the floating point types do not offer. Use BigDecimal for these.

For more on the difference, please see: different-values-for-float-and-double. But mainly remember that Java float is represented internally as a 32-bit representation and double as a 64-bit representation.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • perfect thanks for the link. Could you also explain why my first two tries didn't work out ? Is there an order to follow with the flags ? – hayonj May 12 '13 at 19:45
  • according to the link you sent me ... one guy says this : float can handle about 7 decimal places. A double can handle about 16 decimal places. So I should have been good I only asked for 5 ... Weird ! – hayonj May 12 '13 at 19:49
  • @hayonj: you are confusing decimal places with precision -- they are not the same. Your number is really `3.1513623` times 10 to the 5th power, and so it requires 7 to 8 decimal place precision. – Hovercraft Full Of Eels May 12 '13 at 20:02