BladeOne.php
2 years ago
CSV.php
2 years ago
Calculations.php
2 years ago
Currency.php
1 year ago
Device.php
2 years ago
Device_Cache.php
2 years ago
Dir.php
2 years ago
Number_Formatter.php
2 years ago
Request.php
1 year ago
Salt.php
1 year ago
Security.php
2 years ago
Server.php
2 years ago
Singleton.php
2 years ago
String_Util.php
2 years ago
URL.php
2 years ago
WP_Async_Request.php
2 years ago
WordPress_Site_Date_Format_Pattern.php
2 years ago
Number_Formatter.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Utils; |
| 4 | |
| 5 | use DateTime; |
| 6 | use IAWPSCOPED\Proper\Number; |
| 7 | /** @internal */ |
| 8 | class Number_Formatter |
| 9 | { |
| 10 | /** |
| 11 | * Pass in 90 and get back 1:30. Pass in 121 and get back 2:01. |
| 12 | * |
| 13 | * @param int $seconds |
| 14 | * |
| 15 | * @return string |
| 16 | */ |
| 17 | public static function second_to_minute_timestamp(int $seconds) : string |
| 18 | { |
| 19 | $unix_epoch = new DateTime("@0"); |
| 20 | $now = new DateTime("@{$seconds}"); |
| 21 | $interval = $unix_epoch->diff($now); |
| 22 | return $interval->format('%i:%S'); |
| 23 | } |
| 24 | /** |
| 25 | * @param int|float $number |
| 26 | * @param int $decimals |
| 27 | * |
| 28 | * @return string |
| 29 | */ |
| 30 | public static function percent($number, int $decimals = 0) : string |
| 31 | { |
| 32 | if (\class_exists('\\NumberFormatter')) { |
| 33 | $formatter = new \NumberFormatter(\get_locale(), \NumberFormatter::PERCENT); |
| 34 | $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $decimals); |
| 35 | return $formatter->format($number / 100); |
| 36 | } else { |
| 37 | return \number_format_i18n($number, $decimals) . '%'; |
| 38 | } |
| 39 | } |
| 40 | /** |
| 41 | * @param int|float $number |
| 42 | * @param int $decimals |
| 43 | * |
| 44 | * @return string |
| 45 | */ |
| 46 | public static function decimal($number, int $decimals = 0) : string |
| 47 | { |
| 48 | return \number_format_i18n($number, $decimals); |
| 49 | } |
| 50 | public static function integer($number) : string |
| 51 | { |
| 52 | if ($number < 100000) { |
| 53 | return \number_format_i18n($number, 0); |
| 54 | } |
| 55 | return Number::abbreviate($number, \false); |
| 56 | } |
| 57 | } |
| 58 |