Computer >> 컴퓨터 >  >> 프로그램 작성 >> PHP

PHP에서 금액을 부동 소수점 및 반올림으로 변환하는 방법은 무엇입니까?

<시간/>

금액을 반올림하려면 PHP에서 round()를 사용하십시오. 다음이 입력 값이라고 가정해 보겠습니다. -

$amount=50.78;
$quantity=45.5;

다음과 같이 float로 변환합니다.

$am=(float) round($amount, 4);
$quant=(float) round($quantity, 4);

예시

PHP 코드는 다음과 같습니다

<!DOCTYPE html>
<html>
<body>
<?php
$amount=50.78;
$quantity=45.5;
$am=(float) round($amount, 4);
echo $am,"<br>";
$quant=(float) round($quantity, 4);
echo $quant,"<br>";
$correctAmount=round($am*$quant,4);
echo "The result is=",$correctAmount;
?>
</body>
</html>

출력

그러면 다음과 같은 출력이 생성됩니다.

50.78
45.5
The result is=2310.49