简体   繁体   English

带有特定标志的System.out.printf不返回预期的数字

[英]System.out.printf with specific flags not returning the expected number

I'm just starting out using Java. 我刚开始使用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 瓦伦西亚河谷:315136.23瓦伦西亚河谷,格式: +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. 需按计划申请的服务将保证5个小提琴演奏的时间,并应使每个小提琴演奏者的身份得到提高。

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 巴黎圣母院:315136.23巴黎圣母院,格式: +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 ? 有优先考虑的事情吗?

Don't use float except in very specific circumstances. 除非在非常特殊的情况下,否则不要使用float 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. 这与printf无关,而与使用float作为浮点类型时精度下降有关。 Use double which has double the precision of float or BigDecimal which has a precision that you define. 使用double(精度为float的精度)的两倍或BigDecimal(精度为您定义的精度)。

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. 我认为,我唯一使用浮点数的地方是在调用Java核心类方法时,这些方法需要一个浮点参数(例如某些Swing常量)以及设置Swing字体的大小。 The additional overhead with use of double is minimal. 使用double的额外开销是最小的。 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. 为此使用BigDecimal。

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. 但主要要记住,Java浮点数在内部以32位表示形式表示,在内部以64位表示形式表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM