Round Functions - PHP

All I want to do is use the circular function to display Celsius temperature to one decimal point, but I don't know how to do it.

My code is as follows:

<?php
$fTemp = 0;
while ($fTemp <= 100) {
$cTemp = ($fTemp - 32) * .55;
echo $fTemp." Fahrenheit is equal to ".$cTemp." Celsius<br />"; 
$fTemp++;
}
?>

Any help would be greatly appreciated.

+4
source share
4 answers

Use the second parameter (precision) of the function round().

The code..

echo $fTemp." Fahrenheit is equal to ".round($cTemp,1)." Celsius<br />"; 

1which I used is an accuracy parameter and it will be rounded up to that ...

OUTPUT

0 Fahrenheit is equal to -17.6 Celsius
1 Fahrenheit is equal to -17.1 Celsius
2 Fahrenheit is equal to -16.5 Celsius
3 Fahrenheit is equal to -16 Celsius
4 Fahrenheit is equal to -15.4 Celsius
...... Goes on
+2
source

As in the manual http://www.php.net/manual/en/function.round.php

$cTemp = round(($fTemp - 32) * .55 , 1);
+1
source

, , . , sprintf:

sprintf("%.1f", $fTemp);

% f float double $.1f meand display . : http://www.php.net/manual/en/function.sprintf.php

0

You can use php round function

round ($ cTemp, 1)

0
source

Source: https://habr.com/ru/post/1536392/


All Articles