-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a50d92
commit 6ed860d
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Proper; | ||
|
||
class Number { | ||
/** | ||
* @param int|float $number | ||
* | ||
* @return string | ||
*/ | ||
static public function abbreviate( $number ): string { | ||
$number = (int) $number; | ||
$abbreviations = [ | ||
'' => 1, | ||
'K' => 1000, | ||
'M' => 1000000, | ||
'B' => 1000000000, | ||
'T' => 1000000000000 | ||
]; | ||
|
||
foreach ( $abbreviations as $abbreviation => $abbreviation_value ) { | ||
$upper_range = $abbreviation_value * 1000; | ||
|
||
if ( $number < $upper_range ) { | ||
$decimals = $number < 1000 ? 0 : 1; | ||
$result = $number / $abbreviation_value; | ||
$result = number_format( $result, $decimals ) . $abbreviation; | ||
|
||
// Strip out decimals that are 0 so 1.0T becomes 1T | ||
$result = strpos( $result, '.0' ) === false ? $result : str_replace( '.0', '', $result ); | ||
|
||
return $result; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
use Proper\Number; | ||
|
||
/** | ||
* Tests for Number | ||
*/ | ||
class NumberTest extends WP_UnitTestCase { | ||
|
||
public function test_small_numbers() { | ||
$actual = Number::abbreviate( 1 ); | ||
$expected = '1'; | ||
|
||
$this->assertEquals( $expected, $actual ); | ||
} | ||
|
||
public function test_thousands() { | ||
$actual = Number::abbreviate( 133300 ); | ||
$expected = '133.3K'; | ||
|
||
$this->assertEquals( $expected, $actual ); | ||
} | ||
|
||
public function test_billions() { | ||
$actual = Number::abbreviate( 999000000000 ); | ||
$expected = '999B'; | ||
|
||
$this->assertEquals( $expected, $actual ); | ||
} | ||
|
||
public function test_trillions() { | ||
$actual = Number::abbreviate( 1000000000000 ); | ||
$expected = '1T'; | ||
|
||
$this->assertEquals( $expected, $actual ); | ||
} | ||
|
||
public function test_millions() { | ||
$actual = Number::abbreviate( 1300000 ); | ||
$expected = '1.3M'; | ||
|
||
$this->assertEquals( $expected, $actual ); | ||
} | ||
|
||
public function test_rounding_up() { | ||
$actual = Number::abbreviate( 1250000 ); | ||
$expected = '1.3M'; | ||
|
||
$this->assertEquals( $expected, $actual ); | ||
} | ||
|
||
public function test_rounding_down() { | ||
$actual = Number::abbreviate( 1249999 ); | ||
$expected = '1.2M'; | ||
|
||
$this->assertEquals( $expected, $actual ); | ||
} | ||
} |