Table of Contents
Java 科学计数法
1 科学计数法的概念
1.1 有效数字
1.2 E记号
2 Java中的科学计数法
2.1 NumberFormat
2.2 DecimalFormat
2.3 BigDecimal
Home Java javaTutorial Java scientific notation detailed explanation

Java scientific notation detailed explanation

Mar 19, 2017 am 11:26 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1255
29
C# Tutorial
1228
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles