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 6090823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:15:29+00:00 2026-05-23T12:15:29+00:00

I am calculating the size of a file upload in PHP, to format in

  • 0

I am calculating the size of a file upload in PHP, to format in MB with one decimal place like so:

$metadata['upload_data'] = intval($_FILES['Filedata']['size'] / 104857.6) / 10;

When I echo $metadata['upload_data'] the output is 1.7 as I would expect. But when I serialize the array with serialize($metadata) and save it to a file, the output is:

a:2:{s:7:"uploads";i:11;s:11:"upload_data";d:1.6999999999999999555910790149937383830547332763671875;}

I’m trying to be efficient by storing file sizes in MB not bytes, but this seems worse! Why would PHP store it that way? And am I going about this the right way? Thanks

  • 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-23T12:15:30+00:00Added an answer on May 23, 2026 at 12:15 pm

    From the manual: http://php.net/manual/en/language.types.float.php

    Additionally, rational numbers that
    are exactly representable as floating
    point numbers in base 10, like 0.1 or
    0.7, do not have an exact representation as floating point
    numbers in base 2, which is used
    internally, no matter the size of the
    mantissa. Hence, they cannot be
    converted into their internal binary
    counterparts without a small loss of
    precision. This can lead to confusing
    results: for example,
    floor((0.1+0.7)*10) will usually
    return 7 instead of the expected 8,
    since the internal representation will
    be something like
    7.9999999999999991118….

    I would suggest using json_encode and json_decode if you want to see “1.7” in a serialized version of your array. These functions also end up being quicker than serialize and unserialize and are also easier to read (by easier to read, I mean a person reading them, rather than the machine).

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

Sidebar

Related Questions

I am calculating one parameter(X) this way: <?php $link=mysql_connect(localhost,root,); mysql_select_db(hand); $result = mysql_query(select *
I am trying to get the file size of an image. If I upload
I've got some somewhat hefty string size calculations happening in my app (each one
I have the need to calculate the size of a file I am encrypting
I'm trying to parse some data in a fixed format text file where each
I am attempting to perform a perverse memory trick by calculating the size of
We are developing an application that uploads large files over Internet. File size before
I'm working on research model which outputs results to matlab's .mat file format, and
File I need to modify contains the following: block: 16, size: 16, start: 8,
I have a input field in which users specify file size in Mb, so

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.