PHP number_format() 함수
예시
숫자 형식 지정:
<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>
정의 및 사용
number_format() 함수는 그룹화된 천 단위로 숫자의 형식을 지정합니다.
참고: 이 함수는 1개, 2개 또는 4개의 매개변수(3개가 아님)를 지원합니다.
통사론
number_format(number,decimals,decimalpoint,separator)
매개변수 값
Parameter | Description |
---|---|
number | Required. The number to be formatted. If no other parameters are set, the number will be formatted without decimals and with comma (,) as the thousands separator. |
decimals | Optional. Specifies how many decimals. If this parameter is set, the number will be formatted with a dot (.) as decimal point |
decimalpoint | Optional. Specifies what string to use for decimal point |
separator | Optional. Specifies what string to use for thousands separator. Only the first character of separator is used. For example, "xxx" will give the same output as "x" Note: If this parameter is given, all other parameters are required as well |
기술적 세부 사항
반환 값: | 형식이 지정된 숫자를 반환합니다. |
---|---|
PHP 버전: | 4+ |
변경 로그: | PHP 5.4부터 이 함수는 매개변수 decimalpoint 및 separator 에서 여러 바이트를 지원합니다 . 이전 버전에서는 각 구분 기호의 첫 번째 바이트만 사용되었습니다. |
더 많은 예
예시
가격을 반환하려는 경우: 하나의 매개변수는 숫자를 반올림합니다(소수점 없이 형식 지정됨). 두 개의 매개변수가 원하는 결과를 제공해야 합니다.
<?php
$num = 1999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>
❮ PHP 문자열 참조