# Java: Floating Point Numbers

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java: Float vs. Double

### You're on a roll. Keep up the good work!

Replay
Your next lesson will play in 10 seconds
• 0:04 Floating Point Numbers in Java
• 1:15 Float & Double Data Type
• 3:39 Caution Using Double & Float
• 5:20 Converting Both Ways
• 6:24 Dividing By 0.0
• 7:01 Lesson Summary
Save Save

Want to watch this again later?

Timeline
Autoplay
Autoplay
Speed Speed

#### Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Sudha Aravindan

Sudha has a Doctor of Education Degree and is currently working as a Information Technology Specialist.

In Java, floating point numbers are numbers that have a decimal part to them. Java has two basic floating point data types - float and double. In this lesson, we'll learn what they are and how to use them.

## Floating Point Numbers in Java

Floating point numbers are also called floating point literals in Java. For calculations using decimal values, Java supports two basic data types: float and double. Together, these support decimal or floating point numbers in the Java programming language. Floating point numbers are also known as real numbers and are used when we need precision in calculations. For example, floating point or decimal numbers are used when expressing currency (dollars and cents), measurements (feet and inches), and time (hours, minutes, and seconds). Decimal values are useful because they allow more accuracy and precision.

A float data type is a single precision number format that occupies 4 bytes or 32 bits in computer memory. As we already know, a bit is a single unit that stores a single value 0 or 1. Bits are grouped in groups of 8 to make 1 byte.

A double data type is a double precision number format that occupies 8 bytes or 64 bits in the memory of a computer. This is twice the number of bytes occupied by floats, which is single precision.

Both float and double data types can store positive and negative values.

## Float & Double Data Type

A float data type in Java stores a decimal value with 6-7 total digits of precision. So, for example, 12.12345 can be saved as a float, but 12.123456789 can't be saved as a float. When representing a float data type in Java, we should append the letter f to the end of the data type; otherwise it will save as double.

The default value of a float in Java is 0.0f. Float data type is used when you want to save memory and when calculations don't require more than 6 or 7 digits of precision.

Let's take a look at a few examples:

`float a = 2.356f;`

Here the variable a saves the value 2.356 as a float.

`float x = -125.563f;`

Here the variable x saves the value -125.563 as a float.

`float q = 506.12789f;`

Here the variable q saves the value 506.1247 as a float.

`float r = -101.23;`

Here the variable r isn't stored as a float; instead it's stored as a double because the value 101.23 isn't appended with the letter f.

`float c = 101.123456789123f;`

Here the variable c is attempting to store a float value with 12 digits after the decimal. However, a float in Java can only store a maximum of 7 digits, so this will cause an error.

`float p = 25f;`

Here the variable p will store the value 25 as a float data type in Java.

The double data type stores decimal values with 15-16 digits of precision. Remember that the default value is 0.0d, so this means that if you don't append f or d to the end of the decimal, the value will be stored as a double in Java. Let's now consider a few examples:

`double a = 2.356;`

Here the variable a saves the value 2.356 as a double.

`double x = -125.563`

Here the variable x saves the value -125.563 as a double.

`double q = 506.124789;`

Here the variable q saves the value 506.124789 as a double.

`double r = -101.1234567891234567;`

Here the variable r saves the value 101.1234567891234567 as a double.

`double c = 101.123456789123456789;`

Here the variable c is attempting to store a double value with 18 digits after the decimal. However, a double in Java can only store a maximum of 16 digits, so this will cause an error.

`double p = 25;`

Here the variable p will store the value 25 as a double data type in Java.

## Caution with Using Double & Float

Double and float aren't used to represent values that require a very high precision. So, for instance, to store currency values, it's not a good idea to use double or float; instead Java has a class called BigDecimal that specifies the exact number of digits following the decimal, including rounding up or down.

Consider the following example in Java:

`double k = 3.5 + 4.9;`

You would expect the result to be 8.4, correct? Instead, if you print the result out in Java, you would see that k = 8.3999999999999999;

This is because double can store a maximum of 16 digits after the decimal, and this may be very accurate, but it won't work for calculations involving dollars and cents.

Example with dollars and cents using double:

Suppose you want to find the sum of two purchases you made in a day, and you store the value of the 2 purchases in double data format in Java, like so:

`double purchase1 = 35.40;double purchase2 = 42.90;`

Now to find the sum you declare another double variable sum and compute the result like so:

`double sum = purchase1 + purchase2;`

You would expect the sum in dollars and cents to be \$78.30, correct?

To unlock this lesson you must be a Study.com Member.

### Register to view this lesson

Are you a student or a teacher?

#### See for yourself why 30 million people use Study.com

##### Become a Study.com member and start learning now.
Back
What teachers are saying about Study.com

### Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.