r59715 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r59714‎ | r59715 | r59716 >
Date:23:47, 3 December 2009
Author:tparscal
Status:resolved (Comments)
Tags:
Comment:
Added formatting for contribution total to be in decimal millions like 1 or 1.3 (never 1.0)
Modified paths:
  • /trunk/extensions/CentralNotice/SpecialNoticeText.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/SpecialNoticeText.php
@@ -167,6 +167,13 @@
168168 }
169169
170170 private function formatNum( $num ) {
 171+ // Reduce to millions
 172+ $num *= 0.000001;
 173+ // Grab the fractional part
 174+ $fraction = ( round( $num - 0.0455555, 1, PHP_ROUND_HALF_DOWN ) - floor( $num ) ) * 10;
 175+ // Only append the fractional part if it's more than 0
 176+ $num = floatval( floor( $num ) . ( $fraction ? '.' . $fraction : '' ) );
 177+ // Internationalize
171178 $lang = Language::factory( $this->language );
172179 return $lang->formatNum( $num );
173180 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r59851Picking up fixes from r59715#c4688tomasz22:03, 8 December 2009

Comments

#Comment by Tim Starling (talk | contribs)   23:55, 7 December 2009

PHP_ROUND_HALF_DOWN was only introduced in PHP 5.3.0, which we're not running. But it's broken anyway because your maths is bad. If you make some tables you can see the effect of that arbitrary 0.0455555 offset:

1490000	1.4
1491000	1.4
1492000	1.4
1493000	1.4
1494000	1.4
1495000	1.4
1496000	1.5
1497000	1.5
1498000	1.5
1499000	1.5
1500000	1.5

It would be more conventional, you'd think, to round off 1495000 to 1.5M, or at least to round off 1496000 the same way. May I suggest using code along the lines of:

$num = sprintf("%.1f", $num/1e6);
if ( substr( $num, -2 ) == '.0' ) {
    $num = substr( $num, 0, -2 );
}

It would save a lot of head-scratching.

#Comment by Trevor Parscal (WMF) (talk | contribs)   01:00, 8 December 2009

Yes, that sounds good... Excuses aside, this code was clearly a bit of duct-tape done in a hurry.

Status & tagging log