Number.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. namespace Illuminate\Support;
  3. use Illuminate\Support\Traits\Macroable;
  4. use NumberFormatter;
  5. use RuntimeException;
  6. class Number
  7. {
  8. use Macroable;
  9. /**
  10. * The current default locale.
  11. *
  12. * @var string
  13. */
  14. protected static $locale = 'en';
  15. /**
  16. * The current default currency.
  17. *
  18. * @var string
  19. */
  20. protected static $currency = 'USD';
  21. /**
  22. * Format the given number according to the current locale.
  23. *
  24. * @param int|float $number
  25. * @param int|null $precision
  26. * @param int|null $maxPrecision
  27. * @param string|null $locale
  28. * @return string|false
  29. */
  30. public static function format(int|float $number, ?int $precision = null, ?int $maxPrecision = null, ?string $locale = null)
  31. {
  32. static::ensureIntlExtensionIsInstalled();
  33. $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::DECIMAL);
  34. if (! is_null($maxPrecision)) {
  35. $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maxPrecision);
  36. } elseif (! is_null($precision)) {
  37. $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision);
  38. }
  39. return $formatter->format($number);
  40. }
  41. /**
  42. * Parse the given string according to the specified format type.
  43. *
  44. * @param string $string
  45. * @param int|null $type
  46. * @param string|null $locale
  47. * @return int|float|false
  48. */
  49. public static function parse(string $string, ?int $type = NumberFormatter::TYPE_DOUBLE, ?string $locale = null): int|float|false
  50. {
  51. static::ensureIntlExtensionIsInstalled();
  52. $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::DECIMAL);
  53. return $formatter->parse($string, $type);
  54. }
  55. /**
  56. * Parse a string into an integer according to the specified locale.
  57. *
  58. * @param string $string
  59. * @param string|null $locale
  60. * @return int|false
  61. */
  62. public static function parseInt(string $string, ?string $locale = null): int|false
  63. {
  64. return self::parse($string, NumberFormatter::TYPE_INT32, $locale);
  65. }
  66. /**
  67. * Parse a string into a float according to the specified locale.
  68. *
  69. * @param string $string
  70. * @param string|null $locale
  71. * @return float|false
  72. */
  73. public static function parseFloat(string $string, ?string $locale = null): float|false
  74. {
  75. return self::parse($string, NumberFormatter::TYPE_DOUBLE, $locale);
  76. }
  77. /**
  78. * Spell out the given number in the given locale.
  79. *
  80. * @param int|float $number
  81. * @param string|null $locale
  82. * @param int|null $after
  83. * @param int|null $until
  84. * @return string
  85. */
  86. public static function spell(int|float $number, ?string $locale = null, ?int $after = null, ?int $until = null)
  87. {
  88. static::ensureIntlExtensionIsInstalled();
  89. if (! is_null($after) && $number <= $after) {
  90. return static::format($number, locale: $locale);
  91. }
  92. if (! is_null($until) && $number >= $until) {
  93. return static::format($number, locale: $locale);
  94. }
  95. $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::SPELLOUT);
  96. return $formatter->format($number);
  97. }
  98. /**
  99. * Convert the given number to ordinal form.
  100. *
  101. * @param int|float $number
  102. * @param string|null $locale
  103. * @return string
  104. */
  105. public static function ordinal(int|float $number, ?string $locale = null)
  106. {
  107. static::ensureIntlExtensionIsInstalled();
  108. $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::ORDINAL);
  109. return $formatter->format($number);
  110. }
  111. /**
  112. * Spell out the given number in the given locale in ordinal form.
  113. *
  114. * @param int|float $number
  115. * @param string|null $locale
  116. * @return string
  117. */
  118. public static function spellOrdinal(int|float $number, ?string $locale = null)
  119. {
  120. static::ensureIntlExtensionIsInstalled();
  121. $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::SPELLOUT);
  122. $formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, '%spellout-ordinal');
  123. return $formatter->format($number);
  124. }
  125. /**
  126. * Convert the given number to its percentage equivalent.
  127. *
  128. * @param int|float $number
  129. * @param int $precision
  130. * @param int|null $maxPrecision
  131. * @param string|null $locale
  132. * @return string|false
  133. */
  134. public static function percentage(int|float $number, int $precision = 0, ?int $maxPrecision = null, ?string $locale = null)
  135. {
  136. static::ensureIntlExtensionIsInstalled();
  137. $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::PERCENT);
  138. if (! is_null($maxPrecision)) {
  139. $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maxPrecision);
  140. } else {
  141. $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision);
  142. }
  143. return $formatter->format($number / 100);
  144. }
  145. /**
  146. * Convert the given number to its currency equivalent.
  147. *
  148. * @param int|float $number
  149. * @param string $in
  150. * @param string|null $locale
  151. * @param int|null $precision
  152. * @return string|false
  153. */
  154. public static function currency(int|float $number, string $in = '', ?string $locale = null, ?int $precision = null)
  155. {
  156. static::ensureIntlExtensionIsInstalled();
  157. $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::CURRENCY);
  158. if (! is_null($precision)) {
  159. $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision);
  160. }
  161. return $formatter->formatCurrency($number, ! empty($in) ? $in : static::$currency);
  162. }
  163. /**
  164. * Convert the given number to its file size equivalent.
  165. *
  166. * @param int|float $bytes
  167. * @param int $precision
  168. * @param int|null $maxPrecision
  169. * @return string
  170. */
  171. public static function fileSize(int|float $bytes, int $precision = 0, ?int $maxPrecision = null)
  172. {
  173. $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  174. $unitCount = count($units);
  175. for ($i = 0; ($bytes / 1024) > 0.9 && ($i < $unitCount - 1); $i++) {
  176. $bytes /= 1024;
  177. }
  178. return sprintf('%s %s', static::format($bytes, $precision, $maxPrecision), $units[$i]);
  179. }
  180. /**
  181. * Convert the number to its human-readable equivalent.
  182. *
  183. * @param int|float $number
  184. * @param int $precision
  185. * @param int|null $maxPrecision
  186. * @return bool|string
  187. */
  188. public static function abbreviate(int|float $number, int $precision = 0, ?int $maxPrecision = null)
  189. {
  190. return static::forHumans($number, $precision, $maxPrecision, abbreviate: true);
  191. }
  192. /**
  193. * Convert the number to its human-readable equivalent.
  194. *
  195. * @param int|float $number
  196. * @param int $precision
  197. * @param int|null $maxPrecision
  198. * @param bool $abbreviate
  199. * @return string|false
  200. */
  201. public static function forHumans(int|float $number, int $precision = 0, ?int $maxPrecision = null, bool $abbreviate = false)
  202. {
  203. return static::summarize($number, $precision, $maxPrecision, $abbreviate ? [
  204. 3 => 'K',
  205. 6 => 'M',
  206. 9 => 'B',
  207. 12 => 'T',
  208. 15 => 'Q',
  209. ] : [
  210. 3 => ' thousand',
  211. 6 => ' million',
  212. 9 => ' billion',
  213. 12 => ' trillion',
  214. 15 => ' quadrillion',
  215. ]);
  216. }
  217. /**
  218. * Convert the number to its human-readable equivalent.
  219. *
  220. * @param int|float $number
  221. * @param int $precision
  222. * @param int|null $maxPrecision
  223. * @param array $units
  224. * @return string|false
  225. */
  226. protected static function summarize(int|float $number, int $precision = 0, ?int $maxPrecision = null, array $units = [])
  227. {
  228. if (empty($units)) {
  229. $units = [
  230. 3 => 'K',
  231. 6 => 'M',
  232. 9 => 'B',
  233. 12 => 'T',
  234. 15 => 'Q',
  235. ];
  236. }
  237. switch (true) {
  238. case floatval($number) === 0.0:
  239. return $precision > 0 ? static::format(0, $precision, $maxPrecision) : '0';
  240. case $number < 0:
  241. return sprintf('-%s', static::summarize(abs($number), $precision, $maxPrecision, $units));
  242. case $number >= 1e15:
  243. return sprintf('%s'.end($units), static::summarize($number / 1e15, $precision, $maxPrecision, $units));
  244. }
  245. $numberExponent = floor(log10($number));
  246. $displayExponent = $numberExponent - ($numberExponent % 3);
  247. $number /= pow(10, $displayExponent);
  248. return trim(sprintf('%s%s', static::format($number, $precision, $maxPrecision), $units[$displayExponent] ?? ''));
  249. }
  250. /**
  251. * Clamp the given number between the given minimum and maximum.
  252. *
  253. * @param int|float $number
  254. * @param int|float $min
  255. * @param int|float $max
  256. * @return int|float
  257. */
  258. public static function clamp(int|float $number, int|float $min, int|float $max)
  259. {
  260. return min(max($number, $min), $max);
  261. }
  262. /**
  263. * Split the given number into pairs of min/max values.
  264. *
  265. * @param int|float $to
  266. * @param int|float $by
  267. * @param int|float $start
  268. * @param int|float $offset
  269. * @return array
  270. */
  271. public static function pairs(int|float $to, int|float $by, int|float $start = 0, int|float $offset = 1)
  272. {
  273. $output = [];
  274. for ($lower = $start; $lower < $to; $lower += $by) {
  275. $upper = $lower + $by - $offset;
  276. if ($upper > $to) {
  277. $upper = $to;
  278. }
  279. $output[] = [$lower, $upper];
  280. }
  281. return $output;
  282. }
  283. /**
  284. * Remove any trailing zero digits after the decimal point of the given number.
  285. *
  286. * @param int|float $number
  287. * @return int|float
  288. */
  289. public static function trim(int|float $number)
  290. {
  291. return json_decode(json_encode($number));
  292. }
  293. /**
  294. * Execute the given callback using the given locale.
  295. *
  296. * @param string $locale
  297. * @param callable $callback
  298. * @return mixed
  299. */
  300. public static function withLocale(string $locale, callable $callback)
  301. {
  302. $previousLocale = static::$locale;
  303. static::useLocale($locale);
  304. try {
  305. return $callback();
  306. } finally {
  307. static::useLocale($previousLocale);
  308. }
  309. }
  310. /**
  311. * Execute the given callback using the given currency.
  312. *
  313. * @param string $currency
  314. * @param callable $callback
  315. * @return mixed
  316. */
  317. public static function withCurrency(string $currency, callable $callback)
  318. {
  319. $previousCurrency = static::$currency;
  320. static::useCurrency($currency);
  321. try {
  322. return $callback();
  323. } finally {
  324. static::useCurrency($previousCurrency);
  325. }
  326. }
  327. /**
  328. * Set the default locale.
  329. *
  330. * @param string $locale
  331. * @return void
  332. */
  333. public static function useLocale(string $locale)
  334. {
  335. static::$locale = $locale;
  336. }
  337. /**
  338. * Set the default currency.
  339. *
  340. * @param string $currency
  341. * @return void
  342. */
  343. public static function useCurrency(string $currency)
  344. {
  345. static::$currency = $currency;
  346. }
  347. /**
  348. * Get the default locale.
  349. *
  350. * @return string
  351. */
  352. public static function defaultLocale()
  353. {
  354. return static::$locale;
  355. }
  356. /**
  357. * Get the default currency.
  358. *
  359. * @return string
  360. */
  361. public static function defaultCurrency()
  362. {
  363. return static::$currency;
  364. }
  365. /**
  366. * Ensure the "intl" PHP extension is installed.
  367. *
  368. * @return void
  369. *
  370. * @throws \RuntimeException
  371. */
  372. protected static function ensureIntlExtensionIsInstalled()
  373. {
  374. if (! extension_loaded('intl')) {
  375. $method = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
  376. throw new RuntimeException('The "intl" PHP extension is required to use the ['.$method.'] method.');
  377. }
  378. }
  379. }