Home > Java > javaTutorial > body text

Java scientific notation detailed explanation

高洛峰
Release: 2017-03-19 11:26:11
Original
4130 people have browsed it

Java 科学计数法

1 科学计数法的概念

1.1 有效数字

在一个近似数中,从左边第一个不是0的数字起,到精确到的位数止,这中间的所有数字都叫做这个近似数的有效数字
例如:
890314000保留三位有效数字为8.90×10的8次方 (四舍)
839960000保留三位有效数字为8.40×10的8次方 (五入)
0.00934593保留三位有效数字为9.35×10的-3次方

1.2 E记号

大多数计算器及计算机程序用科学记数法显示非常大和非常小的结果。因为指数上标(例如1011)在屏幕上显示不方便,字母E或e通常是用来代表的十次幂(写作“×10b”),E或e之后的数字是它的指数;换句话说,任何两实数a和b(b应为整数),“aEb”所表示的值是a × 10b。注意,这种用法中字母e不是数学常数e,也不是指数函数exp()(采用用大写字母E显示可以更大程度地避免误解);尽管它也表示指数,但这个符号通常被称为(科学计数法)E或e符号,而不是指数中的底数符号(尽管后者也会出现)。在正式的出版物中尽量不要使用这种显示方法。
注意科学记数法中的e或E与数学常数e或函数exp没有关系。
这种写法是因为一些计算机程序中不方便写上标而产生的,在正式出版物中不应当使用这种写法。
我国国家标准中科学计数法均用a ×10b的形式表示,而不是aEb(参见GB3101-1993,GBT15835-2011,GBT8170-2008)。


2 Java中的科学计数法

在Java中,当Double的取值符合某条件时,将会以科学计数法的方式显示(下面是个人测试的结果,非从文档中得到的结论):

@Test
publicvoid testPrintScientificNotation(){
//整数部分位数大于等于8时开始以科学计数法显示
System.out.println(-12345678.0);
System.out.println(12345678.0);
//整数位为0,当小数位以0开始连续出现大于等于3时开始以科学计数法显示
System.out.println(0.0001);
System.out.println(-0.0001);
}
Copy after login

结果

  1. <span class="pun">-</span><span class="lit">1.2345678E7</span>

  2. <span class="lit">1.2345678E7</span>

  3. <span class="lit">1.0E-4</span>

  4. -<span class="lit">1.0E-4</span>

很多时候,我们需要做一个统一,要么全部以科学计数法输出,要么就全部显示为普通计数。
根据网上的资料,主要提及NumberFormat、DecimalFormat、BigDecimal这三种API实现方式。

2.1 NumberFormat

NumberFormat 是所有数值格式的抽象基类。

publicstaticString scientificNotation2String(Double d,int newValue){
String value =null;
NumberFormat nf =NumberFormat.getInstance();
// 设置此格式中不使用分组
   nf.setGroupingUsed(false);
// 设置数的小数部分所允许的最大位数。
   nf.setMaximumFractionDigits(newValue);
   value = nf.format(d);
return value;
}
Copy after login

如果输入的小数位数,大于设定的最大的小数位数,则会进行四舍五入。

2.2 DecimalFormat

DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。该类设计有各种功能,使其能够解析和格式化任意语言环境中的数,包括对西方语言、阿拉伯语和印度语数字的支持。它还支持不同类型的数,包括整数 (123)、定点数 (123.4)、科学记数法表示的数 (1.23E4)、百分数 (12%) 和金额 ($123)。所有这些内容都可以本地化。

publicstaticString scientificNotation2String(Double d){
String value =null;
DecimalFormat decimalFormat =newDecimalFormat("0.00");//格式化设置  
       value = decimalFormat.format(d);
return value;
}
Copy after login

需要设置模版,指定小数保留的位数,传入数值小数位数超出指定位数,则会进行四舍五入;传入数值小数位不足指定位数,则可以设置补零。
需要将数值转换为科学计数法只须将模版修改即可,例如将模版修改为:0.##E0

2.3 BigDecimal

BigDecimal是不可变的、任意精度的有符号十进制数。

publicstaticString scientificNotation2String(String str){
String value =null;
BigDecimal bd =newBigDecimal(str);
       value = bd.toPlainString();
return value;
}
Copy after login

BigDecimal的构造方法很多,不一定是要传入String类型的值。
BigDecimal中的toString方法和toPlanString方法的区别:

  • toString():返回此BigDecimal的字符串表示形式,如果需要指数,则使用科学计数法

  • toPlainString():返回不带指数字段的此BigDecimal的字符传表示形式

The above is the detailed content of Java scientific notation detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!