我一直在Java中尝试以下代码:
import java.math.*;
public class trial {
public static void main(String[] args) {
// create a BigDecimal object
BigDecimal bg;
// create a Float object
Float f;
float f2 = 35912062;
bg = new BigDecimal(35912062);
// assign the converted value of bg to f
f = bg.floatValue();
String str = "Float value of " + bg + " is " + f;
bg = new BigDecimal(f);
String str2 = "BigDecimal value of " + bg;
// print f value
System.out.println( str );
System.out.println( str2 );
System.out.println( 35912062f );
System.out.println(f2);
}
}
所以,以下是我得到的输出:
Float value of 35912062 is 3.5912064E7
BigDecimal value of 35912064
3.5912064E7
3.5912064E7
它显示浮动的包含范围:3.40282346638528860e 38
这让我很困惑.
任何提供解释的链接都会有所帮助.
编辑:
假设我拿25912062取代35912062,25912062的输出是25912062,但是,35912062的输出是35912064,为什么会这样呢?