Make commafy support negative numbers too,
authorSanthosh Thottingal <santhosh@users.mediawiki.org>
Fri, 11 Nov 2011 17:23:12 +0000 (17:23 +0000)
committerSanthosh Thottingal <santhosh@users.mediawiki.org>
Fri, 11 Nov 2011 17:23:12 +0000 (17:23 +0000)
Add test cases.
Ref Bug 32359  and Followup r97793

languages/Language.php
tests/phpunit/languages/LanguageMlTest.php

index 2e35743..48fd882 100644 (file)
@@ -2722,6 +2722,12 @@ class Language {
                        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
                } else {
                        // Ref: http://cldr.unicode.org/translation/number-patterns
+                       $sign = "";
+                       if ( intval( $_ ) < 0 ) {
+                               // For negative numbers apply the algorithm like positive number and add sign.
+                               $sign =   $_[0];
+                               $_ = substr( $_,1 );
+                       }
                        $numberpart = array();
                        $decimalpart = array();
                        $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches );
@@ -2730,7 +2736,7 @@ class Language {
                        $groupedNumber = ( count( $decimalpart ) > 0 ) ? $decimalpart[0]:"";
                        if ( $groupedNumber  === $_ ) {
                                // the string does not have any number part. Eg: .12345
-                               return $groupedNumber;
+                               return $sign . $groupedNumber;
                        }
                        $start = $end = strlen( $numberpart[0] );
                        while ( $start > 0 ) {
@@ -2750,7 +2756,7 @@ class Language {
                                        $groupedNumber = "," . $groupedNumber;
                                }
                        }
-                       return $groupedNumber;
+                       return $sign . $groupedNumber;
                }
        }
        /**
index a947e78..c659e5c 100644 (file)
@@ -26,5 +26,7 @@ class LanguageMlTest extends MediaWikiTestCase {
                $this->assertEquals( '12,345.56', $this->lang->formatNum( '12345.56' ) );
                $this->assertEquals( '12,34,56,79,81,23,45,678', $this->lang->formatNum( '12345679812345678' ) );
                $this->assertEquals( '.12345', $this->lang->formatNum( '.12345' ) );
+               $this->assertEquals( '-12,00,000', $this->lang->formatNum( '-1200000' ) );
+               $this->assertEquals( '-98', $this->lang->formatNum( '-98' ) );
        }
 }