DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond Partitioning and Z-Order: A Deep Dive into Liquid Clustering for Unity Catalog Managed Tables
  • Five Nonprofit & Charity APIs That Make Due Diligence Way Less Painful for Developers
  • Anthropic’s Model Context Protocol (MCP): A Developer’s Guide to Long-Context LLM Integration
  • Practical Generators in Go 1.23 for Database Pagination

Trending

  • Agentic Systems and Design Patterns
  • Building a Software Factory With AI Agents and Workflows
  • AI-Era Cybersecurity, Part 1: Mapping Glasswing, Daybreak, Lightwell and the Tools Racing to Find and Fix Bugs
  • AI Coding Agent Guardrails: Finding Gaps in Tool Permissions

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. When Reading Excel with POI, Beware of Floating Points

When Reading Excel with POI, Beware of Floating Points

Our problem began when we tried to read a certain cell that contained the value 929 as a numeric field and store it into an integer.

By 
Lieven Doclo
·
Aug. 30, 13 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
48.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Recently, at my current project, we’ve been hunting down a bug in our software. See, our source data comes from Excel files and need to be imported into our system. Since it’s a Java project, we’re using POI.

Our problem began when we tried to read a certain cell that contained the value 929 as a numeric field and store it into an integer. So we did something like this.

int value = new Double(cell.getNumericCellValue()).intValue();

POI only has one way to read numeric cell values, and that’s by using  double. Guess what we got after reading the cell? 928. The reason? The numeric cell value, that shows in Excel as 929, is actually read as 928.99999999… You have to love floating points. You’re probably wondering why we just didn’t read the value as a String value and used Integer.parseInt(...). Well, POI doesn’t allow reading a cell as a String value if it’s actually a numeric cell type.

The solution?

int value = new BigDecimal(d).setScale(0, RoundingMode.HALF_UP).intValue()

The sad part is that this only happens for certain numeric values, if you’re unlucky, the first code example will work (until it suddenly doesn’t anymore). Again, that’s floating point logic. This, by the way, is why I hate floating point and why I think they shouldn’t be allowed in modern development. It’s the principle of least surprise. Groovy, for examples, understands this and considers all decimal values in code as BigDecimal (and allows for easy math operations on those fields, unlike Java). You need to explicitly state your want to use a double.

So remember, when reading Excel numeric values, what you see isn’t always what you get. That, and always use BigDecimal, even if the API forces you to use floating point fields. There’s really no excuse for using double or float.

IT Data (computing) Data Types Groovy (programming language) Java (programming language)

Published at DZone with permission of Lieven Doclo. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Partitioning and Z-Order: A Deep Dive into Liquid Clustering for Unity Catalog Managed Tables
  • Five Nonprofit & Charity APIs That Make Due Diligence Way Less Painful for Developers
  • Anthropic’s Model Context Protocol (MCP): A Developer’s Guide to Long-Context LLM Integration
  • Practical Generators in Go 1.23 for Database Pagination

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook