PHP에서 1달러 금액으로 숫자를 포맷하려면 어떻게 해야 합니까?
숫자를 달러와 센트를 나타내는 문자열로 변환하려면 어떻게 해야 하나요?
eg:
123.45 => '$123.45'
123.456 => '$123.46'
123 => '$123.00'
.13 => '$0.13'
.1 => '$0.10'
0 => '$0.00'
간단한 것을 원하는 경우:
'$' . number_format($money, 2);
PHP에는 money_format()도 있습니다.
다음은 예를 제시하겠습니다.
echo money_format('$%i', 3.4); // echos '$3.40'
이 기능에는 많은 옵션이 있습니다.링크한 메뉴얼을 참조해 주세요.
참고: Windows 에서는 money_format이 정의되어 있지 않습니다.
업데이트: PHP 매뉴얼 사용:https://www.php.net/manual/en/function.money-format.php
경고: 이 함수 [money_format]는 PHP 7.4.0에서 폐지되었습니다.이 기능에 의존하는 것은 매우 권장되지 않습니다.
대신 번호를 조사하십시오.Formatter::formatCurrency.
$number = "123.45";
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
return $formatter->formatCurrency($number, 'USD');
나는 노력했다.money_format()
근데 저는 전혀 안 먹혔어요. 그리고 다음 것도 해봤어요.나한테는 완벽하게 먹혔어당신에게도 잘 되길 바랍니다.:)
이걸 쓰세요.
number_format($money, 2,'.', ',')
10진수 2자리까지 돈 형식으로 돈 번호가 표시됩니다.
PHP 및 C++에서는 printf() 함수를 사용할 수 있습니다.
printf("$%01.2f", $money);
PHP 7.4에서는 money_format() 함수는 권장되지 않습니다.intl 번호로 대체할 수 있습니다.포메터 기능, php-intl 확장자를 사용하도록 설정해 주세요.많은 커스터마이즈 기능을 얻을 수 있기 때문에 적은 노력과 가치가 있습니다.
$f = new NumberFormatter("en", NumberFormatter::CURRENCY);
$f->formatCurrency(12345, "USD"); // Outputs "$12,345.00"
Darryl Hein이 언급한 것처럼 7.4에서도 여전히 빠르게 동작할 수 있는 방법은 다음과 같습니다.
'$' . number_format($money, 2);
php.ini에서 이것을 추가합니다(없을 경우).
#windows
extension=php_intl.dll
#linux
extension=php_intl.so
다음 작업을 수행합니다.
$amount = 123.456;
// for Canadian Dollars
$currency = 'CAD';
// for Canadian English
$locale = 'en_CA';
$fmt = new \NumberFormatter( $locale, \NumberFormatter::CURRENCY );
echo $fmt->formatCurrency($amount, $currency);
/* Just Do the following, */
echo money_format("%(#10n","123.45"); //Output $ 123.45
/* If Negative Number -123.45 */
echo money_format("%(#10n","-123.45"); //Output ($ 123.45)
언급URL : https://stackoverflow.com/questions/294865/how-do-i-format-a-number-to-a-dollar-amount-in-php
'itsource' 카테고리의 다른 글
Java RegEx는 대소문자를 구분하지 않습니까? (0) | 2022.09.14 |
---|---|
com.mysql.jdbc 클래스를 로드하고 있습니다.운전기사님.이것은 권장되지 않습니다.새로운 드라이버 클래스는 'com.mysql.cj.jdbc'입니다.드라이버 (0) | 2022.09.14 |
업데이트 후 Android Studio에서 리소스 오류 발생:리소스를 찾을 수 없습니다. (0) | 2022.09.14 |
ORDERBY 사용 시 MySQL Slow JOIN 쿼리 (0) | 2022.09.14 |
컨텍스트 경로 없이 요청 URI를 얻는 방법 (0) | 2022.09.14 |