PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 4, 2022

[FIXED] How to fix PhpExcel extra decimal points bug?

 October 04, 2022     excel, floating-point, php, phpexcel, precision     No comments   

Issue

I'm using PHPExcel for a client project I'm working on. There's this bug I've been facing where extra decimal places are added randomly for some cells while reading the excel file. For example, instead of 56.25, I get 56.24999999999. I've traced this problem to PHPExcel itself and I've spent a lot of time looking through the documentation but I haven't found anything that can solve this. I can't just round off the numbers to a given decimal point because it happens randomly and different cells have numbers with different decimal points. Please help!!!

EDIT: It is critical that the decimal points remain exactly as they were manually input. So if the client manually inputs 34.25 in one cell and then 51.3456 in a other cell, they should remain exactly like that! So rounding off isn't the ideal solution here.


Solution

After reading the comment by Mark Baker,

These aren't random; decimal float values cannot always be represented exactly on computers that use binary representation.... this is not a PHPExcel problem, or even a PHP problem.... it's a well-documented digital computer problem; and the solutions for formatting are provided by sprint() or number_format() –

I discovered that making sure all numbers are saved as text solves this problem. PHP will read them as strings hence its weird floating point shenanigans won't be a problem.



Answered By - ninjacoder
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing