Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is a floor in this (pun intended), even chatGPT can't explain it #3044

Closed
perlsol opened this issue Dec 27, 2023 · 1 comment
Closed

Comments

@perlsol
Copy link

perlsol commented Dec 27, 2023

function abbreviateNumber($number) {
if ($number >= 1000000000) {
$abbreviated = number_format($number / 1000000000, 1) . 'B';
} elseif ($number >= 1000000) {
$abbreviated = number_format($number / 1000000, 1) . 'M';
} elseif ($number >= 1000) {
$abbreviated = number_format($number / 1000, 0) . 'K';
} else {
$abbreviated = $number;
}
return $abbreviated;
}

function countLines($filename) {
$lineCount = 0;

$file = new SplFileObject($filename);
$file->setFlags(SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);

foreach ($file as $line) {
    if ($line !== false) {
        $lineCount++;
    }
}

return $lineCount;

}

$filename = 'archive.tmp';
$lines = countLines($filename);
$roundedDown = floor($lines / 1000) * 1000;
$abbreviatedNumber = abbreviateNumber($roundedDown);

echo $abbreviatedNumber . "+ IP's LISTED";

archive.tmp contains 1056241 ip addresses, why does abbreviatedNumber say 1.1M when it should say 1.0M
My guess is it is not rounding down, it said 1.0 when it was just over 1M IP addresses in the file

@TimWolla
Copy link
Member

My guess is it is not rounding down,

You're rounding to the nearest 1000, not to the nearest million. In any case this is neither a documentation issue, nor a bug in PHP. Please use some forum, such as StackOverflow, if you need programming help.

@TimWolla TimWolla closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2023
@TimWolla TimWolla added invalid This doesn't seem right Status: Invalid and removed invalid This doesn't seem right labels Dec 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants