Whats new in PHP 8.4
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
php84/libs/Dollars.php

23 lines
702 B

<?php
namespace libs;
use BcMath\Number;
class Dollars {
private function moneyAsFloat(Number $money): float {
// Remove any existing currency symbols and thousands separators
$t = preg_replace('/[^\d.-]/', '', $money);
return (float)$t;
}
public function formatAsUSD(Number $money): string {
return '$' . number_format($this->moneyAsFloat($money), 2, '.', ',');
}
public function intlUSD(Number $money): string {
if (class_exists('NumberFormatter')) {
$formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
return $formatter->formatCurrency($this->moneyAsFloat($money), 'USD');
}
}
}