Java Lesson Two: integers, floating-point numbers

Source: Internet
Author: User

Floating point number

The number with the decimal point. The word floating point is meant to mean that the decimal point is floating and is a way of expressing non-integers (including fractions and irrational numbers) within the computer. The other is called a fixed-point number, but no fixed-point number is encountered in Java. People use the word floating point to express all the numbers with a decimal point.

When floating-point numbers and integers are put together, Java converts integers into floating-point numbers and then operations on floating-point numbers.

A double is usually used to denote the type of floating-point number

Floating-point calculations are error-based, and integers are used when we need accurate calculations.

Integer types cannot express numbers that have fractional parts. The strange thing about a computer that has a pure integer is that it's a lot faster and takes up a little bit of space. In fact, People's daily life is still a lot of pure integer calculation, so the use of integers is still very large.

10 and 10.0 are completely different two numbers in Java.

Example: height-foot conversion, 5 ' 7 "How much is the height of the meter?

Formula: (5+7/12) *0.3048=1.7018

public class Main {public static void main (string[] args) {//TODO auto-generated method Stubint foot;int inch; Scanner in = new Scanner (system.in); foot = In.nextint (); inch = In.nextint (); System.out.println ((foot + INCH/12) *0.3048);}}

Output Result:

5 7

1.524

Why not 1.7018? Because the inch defines an int integer, and the integer operation can only get an integer, the operation INCH/12 the last rounding to 0, so the final result is wrong. On the basis of the above code, there are two methods: one is to change the INCH/12 to inch/12.0 (integer and floating-point operation results are floating-point numbers), another method is to define the inch as a floating-point number double, to the inch assignment 7, automatically into the 7.0来 participate in the operation.

Therefore, the following two kinds of code can get the correct result:

(1) inch/12.0

public class Main {public static void main (string[] args) {//TODO auto-generated method Stubint foot;int inch; Scanner in = new Scanner (system.in); foot = In.nextint (); inch = In.nextint (); System.out.println ((foot + inch/12.0) *0.3048);}}

(2) Double inch:

public class Main {public static void main (string[] args) {//TODO auto-generated method Stubint foot;double inch; Scanner in = new Scanner (system.in); foot = In.nextint (); inch = In.nextint (); System.out.println ((foot + INCH/12) *0.3048);}}


This article is from the "Office software" blog, please be sure to keep this source http://xiaobenny.blog.51cto.com/2638228/1729933

Java Lesson Two: integers, floating-point numbers

Related Article

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.