Skip to content

Commit

Permalink
Add number abbreviator
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjmead committed May 25, 2023
1 parent 6a50d92 commit 6ed860d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,20 @@ $gatekeeper = new Gatekeeper('option_name', $period);
**Why not use WordPress cron?**

WordPress ships with built-in support for cron tasks, but it's a cumbersome API that requires the manual scheduling and unscheduling of tasks. Gatekeeper is designed to be a lightweight version that you can easily add, remove, and modify as needed. There's no need to add separate code for scheduling an unscheduling tasks.

## Number

### Abbreviate

Abbreviate large numbers converting numbers such as `23000` into `23K`.


```php
<?php

use Proper\Number;

Number::abbreviate(1260000); // 1.3M

Number::abbreviate(133800); // 133.8K
```
36 changes: 36 additions & 0 deletions source/Number.php
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;
}
}
}
}
58 changes: 58 additions & 0 deletions tests/number-test.php
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 );
}
}

0 comments on commit 6ed860d

Please sign in to comment.