Output Format of floating point numbers in Java and possible problems

Source: Internet
Author: User
Recently, a database SQL statement has been used in the project. This SQL statement is dynamically generated, similar to the following method:
Double quantity = ...;
String SQL = "insert into" + tablename + "(ID, name, quantity) values ("
+ ID + ", '" + name + "'," + quantity + ")";

The system has been running normally for more than a year, and a system error occurred recently. Due to the lack of sufficient debugging information in the production environment, debugging is very difficult. After hard tracking and debugging, the problem occurs in the string representation of the double-type quantity. In the recent system, quantity has encountered a relatively large value (greater than 10 to the power of 7), while the double-type quantity is resolved to a scientific notation similar to "1.153e7, although most of the SQL-compatible drivers basically understand this scientific notation form of digital representation, there are still some unsupported (whether there are provisions in the SQL specification I do not know, you are welcome to verify ). For some drives that do not support this digital representation, the problem arises. It will regard it as an invalid number and report an error.

In Java, floating point numbers include basic float, double, and object packaging float and double. For the output of these floating point numbers, whether explicitly or implicitly calling tostring () the output format is as follows:
?

  • If the absolute value is greater than 0.001 or less than 10000000, it is expressed as a regular decimal number.
    ?
  • If it is out of the preceding range, scientific notation is used. It is similar to the form of 1.234e8.

If you know this clearly, you can avoid some problems. In addition, for most enterprise applications, users prefer to use ordinary decimal representation instead of scientific notation, in actual projects, we often encounter the need to unify the default representation of floating point numbers in Java into ordinary decimal output. You can use Java. text. decimalformat conversion. For example, convert the double type to retain the four decimal places:

Decimalformat df =NewDecimalformat ("#. 0000 ");
DoubleD = 12345678.12345;
String dstr = DF. Format (d );

In this way, dstr is changed to 1234567.1234 instead of 1.234567812345e7.

Understand the output representation of floating point numbers in Java, and keep it easy to remember. Especially for enterprise applications that target common users, it is very important to format floating point numbers in proper places. For some SQL drivers, you also need to know whether to identify this scientific notation form. Otherwise, it means that an irregular bomb is planted for the system, and you do not know when the explosion occurs, maybe a few years later, it will be very difficult to find problems and correct programs at that time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.