r102802 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102801‎ | r102802 | r102803 >
Date:17:23, 11 November 2011
Author:santhosh
Status:ok (Comments)
Tags:
Comment:
Make commafy support negative numbers too,
Add test cases.
Ref Bug 32359 and Followup r97793
Modified paths:
  • /trunk/phase3/languages/Language.php (modified) (history)
  • /trunk/phase3/tests/phpunit/languages/LanguageMlTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/languages/LanguageMlTest.php
@@ -26,5 +26,7 @@
2727 $this->assertEquals( '12,345.56', $this->lang->formatNum( '12345.56' ) );
2828 $this->assertEquals( '12,34,56,79,81,23,45,678', $this->lang->formatNum( '12345679812345678' ) );
2929 $this->assertEquals( '.12345', $this->lang->formatNum( '.12345' ) );
 30+ $this->assertEquals( '-12,00,000', $this->lang->formatNum( '-1200000' ) );
 31+ $this->assertEquals( '-98', $this->lang->formatNum( '-98' ) );
3032 }
3133 }
Index: trunk/phase3/languages/Language.php
@@ -2722,6 +2722,12 @@
27232723 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
27242724 } else {
27252725 // Ref: http://cldr.unicode.org/translation/number-patterns
 2726+ $sign = "";
 2727+ if ( intval( $_ ) < 0 ) {
 2728+ // For negative numbers apply the algorithm like positive number and add sign.
 2729+ $sign = $_[0];
 2730+ $_ = substr( $_,1 );
 2731+ }
27262732 $numberpart = array();
27272733 $decimalpart = array();
27282734 $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches );
@@ -2730,7 +2736,7 @@
27312737 $groupedNumber = ( count( $decimalpart ) > 0 ) ? $decimalpart[0]:"";
27322738 if ( $groupedNumber === $_ ) {
27332739 // the string does not have any number part. Eg: .12345
2734 - return $groupedNumber;
 2740+ return $sign . $groupedNumber;
27352741 }
27362742 $start = $end = strlen( $numberpart[0] );
27372743 while ( $start > 0 ) {
@@ -2750,7 +2756,7 @@
27512757 $groupedNumber = "," . $groupedNumber;
27522758 }
27532759 }
2754 - return $groupedNumber;
 2760+ return $sign . $groupedNumber;
27552761 }
27562762 }
27572763 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r102998MFT r98140 r100023 r102786 r102802 r102893nikerabbit18:20, 14 November 2011
r103128Follow up r102802. Bug 32359...santhosh04:28, 15 November 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r97793Add support for Number grouping(commafy) based on CLDR number grouping patter...santhosh05:01, 22 September 2011

Comments

#Comment by Liangent (talk | contribs)   05:54, 12 November 2011

by the way I think you can add some test cases for input value of int type.

Status & tagging log