By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,747 Members | 2,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,747 IT Pros & Developers. It's quick & easy.

Floating point Division Help

P: 6
Hey, is there any reason why in php5 if I have

$FreeSpace = 1633783808;
$Size = 12889010176;

and I am doing $FreeSpace/$Size I am getting 0.76 (I set the precision to two)

and if I am doing
$FreeSpace = 267488886784;
$Size = 292309999616;
$FreeSpace/$Size = 1

I am kind of new to php and I have no idea why this is not doing the math right. In the end I am trying to get % used of a hard disk (1-%of free disk space) but I need this to produce the right numbers first.
Thanks in advance.
Feb 29 '08 #1
Share this Question
Share on Google+
1 Reply


ronverdonk
Expert 2.5K+
P: 4,258
You are using floats, not ints.
Your first calculate does not yield 0.76 but 0.1267578957337, or rounded 0.13.
The second calculate yields 0.91508633688684, but you have probably not specified the second argument of the round function. Using the proper round's
[php]<?php
$FreeSpace = 1633783808;
$Size = 12889010176;
echo round($FreeSpace /$Size,2); // 0.13 (0.1267578957337)
$FreeSpace = 267488886784;
$Size = 292309999616;
echo round($FreeSpace /$Size,2); // 0.92 (0.91508633688684)
?>[/php]Ronald
Feb 29 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.