Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 915015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:49:01+00:00 2026-05-15T17:49:01+00:00

I was reading that you have to be careful with floating point comparisons and

  • 0

I was reading that you have to be careful with floating point comparisons and calculations in PHP. I’m not an expert at PHP so I thought I would ask the community what is the best way to do accurate decimal calculations in PHP.

I’m developing a timecard application where it calculates all the time entered for the week. Anything over 40 hours is considered overtime. I then want to distribute that overtime back across each time entry based on the percentage that entry is of the total time.

Example:

Monday = 10 hours
Total Time = 50 hours
Total Overtime = 10 hours

Percent of Total time (0.2) = Monday (10 hours) / Total Time (50 hours)
Overtime to Allocate to Monday (2 hours) = Total Overtime (10 hours) * Percent of Total Time (0.2)

Now make a new entry for Monday with 2 hours of overtime and subtract 2 hours from original entry.

New Entries:
Monday (8 hours) = Monday – Overtime to Allocate to Monday (2 hours)
Monday Overtime = 2 hours

Things don’t seem to bad when using integers as your original entries but what will happen when I start using floats and have multiple time entries per day? Each time entry will have part of the total overtime allocated to it but I need to make sure not to allocate more overtime then is available and I also need to make sure the total time for each day and for the whole timecard is the same as originally entered. I read comparing floats in php are not safe. How should I do the calculation. Also, the final entered numbers have to be rounded to 2 decimal points (.##)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:49:02+00:00Added an answer on May 15, 2026 at 5:49 pm

    Read The Floating-Point Guide to understand why exactly you have to be careful with mixing floating-point numbers and the concept of “decimals” (which has absolutely nothing to do with PHP specifically).

    It also has a cheat-sheet for PHP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading log files that have been compressed with gzip in a php
I have been reading that creating dependencies by using static classes/singletons in code, is
I have been reading that using HibernateDaoSupport isn't a good idea, so I need
I have a python ndarray temp in some code I'm reading that suffers this:
I was reading today about OOCSS which says by using that approach have 2
I have a C++ program that is reading in info from a file, that
I have a perl script that's reading an INI file like this: [placeholder_title] Hostname
I was reading the OS theory in which I found that a process have
I have DataReceviedHandler, that is reading the data from the Rx terminal. I will
I'm sort of lost on this. I have an application that is reading from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.