eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

Dealing with numerical data often requires precision handling. One common scenario arises when we need to check whether a double is, in fact, a mathematical integer.

In this tutorial, we’ll explore the various techniques available to perform this check, ensuring accuracy and flexibility in our numeric evaluations.

2. Introduction to the Problem

First, as we know, a double is a floating-point data type that can represent fractional values and has a broader range than Java int or Integer. On the other hand, a mathematical integer is a whole number data type that cannot store fractional values.

A double can be considered as representing a mathematical integer when the value after the decimal point is negligible or non-existent. This implies that the double holds a whole number without any fractional component. For example, 42.0D is actually an integer (42). However, 42.42D isn’t.

In this tutorial, we’ll learn several approaches to checking whether a double is a mathematical integer.

3. The Special Double Values: NaN and Infinity

Before we dive into checking if a double is an integer, let’s first look at a few special double values: Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, and Double.NaN.

Double.NaN means the value is “not a number”. Thus, it’s not an integer either.

On the other hand, both Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY isn’t a concrete number in the traditional sense. They represent infinity, a special value that indicates a result of a mathematical operation that exceeds the maximum representable finite value for a double-precision floating-point number. Therefore, these two infinity values aren’t integers either.

Further, the Double class provides the isNan() and isInfinite() methods to tell if a Double object is NaN or infinite. So, we can first perform these special values checks before we check if a double is an integer.

For simplicity, let’s create a method to execute this task so that it can be reused in our code examples:

boolean notNaNOrInfinity(double d) {
    return !(Double.isNaN(d) || Double.isInfinite(d));
}

4. Casting the double to int

To determine whether a double is an integer, the most straightforward idea is probably first casting the double to an int and then compare the casted int to the original doubleIf their values are equal, the double is an integer.

Next, let’s implement and test this idea:

double d1 = 42.0D;
boolean d1IsInteger = notNaNOrInfinity(d1) && (int) d1 == d1;
assertTrue(d1IsInteger);

double d2 = 42.42D;
boolean d2IsInteger = notNaNOrInfinity(d2) && (int) d2 == d2;
assertFalse(d2IsInteger);

As the test shows, this approach does the job.

However, as we know, double’s range is wider than int‘s in Java. So, let’s write a test to check what happens if the double exceeds the int range:

double d3 = 2.0D * Integer.MAX_VALUE;
boolean d3IsInteger = notNaNOrInfinity(d3) && (int) d3 == d3;
assertTrue(!d3IsInteger); // <-- fails if exceeding Integer's range

In this test, we assign 2.0D * Integer.MAX_VALUE to the double d3. Obviously, this value is a mathematical integer but out of Java’s integer range. So, it turns out that this approach won’t work if the given double is outside of the Java integer’s range.

Moving forward, let’s explore alternative solutions that address scenarios where doubles surpass the range of integers.

5. Using the Modulo Operator ‘%

We’ve mentioned that if a double is an integer, it doesn’t have a fractional part. Therefore, we can test if the double is divisible by 1. To do that, we can use the modulo operator:

double d1 = 42.0D;
boolean d1IsInteger = notNaNOrInfinity(d1) && (d1 % 1) == 0;
assertTrue(d1IsInteger);

double d2 = 42.42D;
boolean d2IsInteger = notNaNOrInfinity(d2) && (d2 % 1) == 0;
assertFalse(d2IsInteger);

double d3 = 2.0D * Integer.MAX_VALUE;
boolean d3IsInteger = notNaNOrInfinity(d3) && (d3 % 1) == 0;
assertTrue(d3IsInteger);

As the test shows, this approach works even if the double is out of the integer range.

6. Rounding the double

The standard Math class provided a series of rounding methods:

  • ceil() – Examples: ceil(42.0001) = 43; ceil(42.999) = 43
  • floor() – Examples: floor(42.0001) = 42; floor(42.9999) = 42
  • round() – Examples: round(42.4) = 42; round(42.5) = 43
  • rint() – Examples: rint(42.4) = 42; rint(42.5) = 43

We won’t go into details of Math rounding methods in the list. All these methods share a common characteristic: they round the provided double to a close mathematical integer.

If a double represents a mathematical integer, after passing it to any rounding method in the list above, the result must be equal to the input double, for example:

  • ceil(42.0) = 42
  • floor(42.0) = 42
  • round(42.0) = 42
  • rint(42.0) = 42

Therefore, we can use any rounding method to perform the check.

Next, let’s take the Math.floor() as an example to demonstrate how this is done:

double d1 = 42.0D;
boolean d1IsInteger = notNaNOrInfinity(d1) && Math.floor(d1) == d1;
assertTrue(d1IsInteger);

double d2 = 42.42D;
boolean d2IsInteger = notNaNOrInfinity(d2) && Math.floor(d2) == d2;
assertFalse(d2IsInteger);

double d3 = 2.0D * Integer.MAX_VALUE;
boolean d3IsInteger = notNaNOrInfinity(d3) && Math.floor(d3) == d3;
assertTrue(d3IsInteger);

As demonstrated by the test results, this solution remains effective even when the double exceeds the integer range.

Of course, if we want, we can replace the floor() method with ceil(), round(), or rint().

7. Using Guava

Guava is a widely used open-source library of common utilities. Guava’s DoubleMath class provides the isMathematicalInteger() method. The method name implies that it’s exactly the solution we are looking for.

To include Guava, we need to add its dependency to our pom.xml:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>33.0.0-jre</version>
</dependency>

The latest version information can be found on Maven Repository.

Next, let’s write a test to verify whether DoubleMath.isMathematicalInteger() works as expected:

double d1 = 42.0D;
boolean d1IsInteger = DoubleMath.isMathematicalInteger(d1);
assertTrue(d1IsInteger);

double d2 = 42.42D;
boolean d2IsInteger = DoubleMath.isMathematicalInteger(d2);
assertFalse(d2IsInteger);

double d3 = 2.0D * Integer.MAX_VALUE;
boolean d3IsInteger = DoubleMath.isMathematicalInteger(d3);
assertTrue(d3IsInteger);

As evidenced by the test results, the method consistently produces the expected result, no matter whether the input double falls within or outside the range of Java integers.

Sharp eyes might have noticed we didn’t call notNaNOrInfinity() in the test above for NaN and infinity check. This is because the DoubleMath.isMathematicalInteger() method handles NaN and infinity too:

boolean isInfinityInt = DoubleMath.isMathematicalInteger(Double.POSITIVE_INFINITY);
assertFalse(isInfinityInt);

boolean isNanInt = DoubleMath.isMathematicalInteger(Double.NaN);
assertFalse(isNanInt);

8. Conclusion

In this article, we first discussed what “a double represents a mathematical integer” means. Then, we’ve explored different ways to check whether a double indeed qualifies as a mathematical integer.

While the straightforward casting of a double to an int (i.e., theDouble == (int) theDouble) may seem intuitive, its limitation lies in its inability to handle cases where theDouble falls beyond the range of Java integers.

To address this limitation, we looked at moduli and rounding approaches, which can correctly handle doubles whose values extend beyond the integer range. Furthermore, we demonstrated the DoubleMath.isMathematicalInteger() method from Guava as an additional, robust solution to our problem.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments