Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解析有效数字后获取垃圾?_Java_Parsing_Floating Point - Fatal编程技术网

Java 解析有效数字后获取垃圾?

Java 解析有效数字后获取垃圾?,java,parsing,floating-point,Java,Parsing,Floating Point,好的,我有一个测试程序: public class ParseTest { public static void main(String[] args){ String s = "-0.000051"; System.out.println(s + " != " + Float.parseFloat(s)); } } 而且它并不是把同一个数字放在两边。这是输出: -0.000051 != -5.1E-5 那么,为什么它不在两侧输出相同的数字呢?

好的,我有一个测试程序:

public class ParseTest {
    public static void main(String[] args){
        String s = "-0.000051";
        System.out.println(s + " != " + Float.parseFloat(s));
    }
}
而且它并不是把同一个数字放在两边。这是输出:

-0.000051 != -5.1E-5

那么,为什么它不在两侧输出相同的数字呢?

它只是在输出相同的数字

例如:

100 = 1E2 


如何获得相同的表示?

如果需要相同的表示法,请使用
BigDecimal

System.out.println(s + " != " + new BigDecimal(s));

谁正在将其转换为此格式?

例如,将基元转换为字符串时

float f = 100.123;
String result = "" + f; 

它调用which inturns调用which inturn调用this is this this this this this this this this this this this this this this this this this this this this this this this this this this this this this this this this this this this this。是同一个号码。看。其他输出,看。这是相同的数字,伙计。一个是十进制,另一个是指数:两个符号显示相同的值!您的“println()”隐式执行“Float.toString()”。如果需要不同的表示法,请查看“String.format()”。例如:String.format(“%.2f”,floatValue);
float f = 100.123;
String result = "" + f;