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 );
$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 ) {
$groupedNumber = "," . $groupedNumber;
}
}
- return $groupedNumber;
+ return $sign . $groupedNumber;
}
}
/**
$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' ) );
}
}