Jump to content

Can anyone help a newbie please?


koohoofoo

Recommended Posts

I am VERY new to PHP and I am working on an assignment for school and I have most of the assignment done, but the only thing that I can not figure out is the last step. It says to use the round() function to display the Celsius temperature to one place after the decimal point.

 

No matter where I look I can not figure out how to do this. The rest of my code works well. Can anyone help me and show me how to do this round statement?

 

heres what I have:

 

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

 

Link to comment
Share on other sites

When I run the code....This is my output.....

 

0 Fahrenheit is equal to -17.6 Celsius
1 Fahrenheit is equal to -17.05 Celsius
2 Fahrenheit is equal to -16.5 Celsius
3 Fahrenheit is equal to -15.95 Celsius
4 Fahrenheit is equal to -15.4 Celsius
5 Fahrenheit is equal to -14.85 Celsius
6 Fahrenheit is equal to -14.3 Celsius
7 Fahrenheit is equal to -13.75 Celsius
8 Fahrenheit is equal to -13.2 Celsius
9 Fahrenheit is equal to -12.65 Celsius
10 Fahrenheit is equal to -12.1 Celsius
11 Fahrenheit is equal to -11.55 Celsius
12 Fahrenheit is equal to -11 Celsius
13 Fahrenheit is equal to -10.45 Celsius
14 Fahrenheit is equal to -9.9 Celsius
15 Fahrenheit is equal to -9.35 Celsius
16 Fahrenheit is equal to -8.8 Celsius
17 Fahrenheit is equal to -8.25 Celsius
18 Fahrenheit is equal to -7.7 Celsius
19 Fahrenheit is equal to -7.15 Celsius
20 Fahrenheit is equal to -6.6 Celsius
21 Fahrenheit is equal to -6.05 Celsius
22 Fahrenheit is equal to -5.5 Celsius
23 Fahrenheit is equal to -4.95 Celsius
24 Fahrenheit is equal to -4.4 Celsius
25 Fahrenheit is equal to -3.85 Celsius
26 Fahrenheit is equal to -3.3 Celsius
27 Fahrenheit is equal to -2.75 Celsius
28 Fahrenheit is equal to -2.2 Celsius
29 Fahrenheit is equal to -1.65 Celsius
30 Fahrenheit is equal to -1.1 Celsius
31 Fahrenheit is equal to -0.55 Celsius
32 Fahrenheit is equal to 0 Celsius
33 Fahrenheit is equal to 0.55 Celsius
34 Fahrenheit is equal to 1.1 Celsius
35 Fahrenheit is equal to 1.65 Celsius
36 Fahrenheit is equal to 2.2 Celsius
37 Fahrenheit is equal to 2.75 Celsius
38 Fahrenheit is equal to 3.3 Celsius
39 Fahrenheit is equal to 3.85 Celsius
40 Fahrenheit is equal to 4.4 Celsius
41 Fahrenheit is equal to 4.95 Celsius
42 Fahrenheit is equal to 5.5 Celsius
43 Fahrenheit is equal to 6.05 Celsius
44 Fahrenheit is equal to 6.6 Celsius
45 Fahrenheit is equal to 7.15 Celsius
46 Fahrenheit is equal to 7.7 Celsius
47 Fahrenheit is equal to 8.25 Celsius
48 Fahrenheit is equal to 8.8 Celsius
49 Fahrenheit is equal to 9.35 Celsius
50 Fahrenheit is equal to 9.9 Celsius
51 Fahrenheit is equal to 10.45 Celsius
52 Fahrenheit is equal to 11 Celsius
53 Fahrenheit is equal to 11.55 Celsius
54 Fahrenheit is equal to 12.1 Celsius
55 Fahrenheit is equal to 12.65 Celsius
56 Fahrenheit is equal to 13.2 Celsius
57 Fahrenheit is equal to 13.75 Celsius
58 Fahrenheit is equal to 14.3 Celsius
59 Fahrenheit is equal to 14.85 Celsius
60 Fahrenheit is equal to 15.4 Celsius
61 Fahrenheit is equal to 15.95 Celsius
62 Fahrenheit is equal to 16.5 Celsius
63 Fahrenheit is equal to 17.05 Celsius
64 Fahrenheit is equal to 17.6 Celsius
65 Fahrenheit is equal to 18.15 Celsius
66 Fahrenheit is equal to 18.7 Celsius
67 Fahrenheit is equal to 19.25 Celsius
68 Fahrenheit is equal to 19.8 Celsius
69 Fahrenheit is equal to 20.35 Celsius
70 Fahrenheit is equal to 20.9 Celsius
71 Fahrenheit is equal to 21.45 Celsius
72 Fahrenheit is equal to 22 Celsius
73 Fahrenheit is equal to 22.55 Celsius
74 Fahrenheit is equal to 23.1 Celsius
75 Fahrenheit is equal to 23.65 Celsius
76 Fahrenheit is equal to 24.2 Celsius
77 Fahrenheit is equal to 24.75 Celsius
78 Fahrenheit is equal to 25.3 Celsius
79 Fahrenheit is equal to 25.85 Celsius
80 Fahrenheit is equal to 26.4 Celsius
81 Fahrenheit is equal to 26.95 Celsius
82 Fahrenheit is equal to 27.5 Celsius
83 Fahrenheit is equal to 28.05 Celsius
84 Fahrenheit is equal to 28.6 Celsius
85 Fahrenheit is equal to 29.15 Celsius
86 Fahrenheit is equal to 29.7 Celsius
87 Fahrenheit is equal to 30.25 Celsius
88 Fahrenheit is equal to 30.8 Celsius
89 Fahrenheit is equal to 31.35 Celsius
90 Fahrenheit is equal to 31.9 Celsius
91 Fahrenheit is equal to 32.45 Celsius
92 Fahrenheit is equal to 33 Celsius
93 Fahrenheit is equal to 33.55 Celsius
94 Fahrenheit is equal to 34.1 Celsius
95 Fahrenheit is equal to 34.65 Celsius
96 Fahrenheit is equal to 35.2 Celsius
97 Fahrenheit is equal to 35.75 Celsius
98 Fahrenheit is equal to 36.3 Celsius
99 Fahrenheit is equal to 36.85 Celsius
100 Fahrenheit is equal to 37.4 Celsius

 

I just need to figure out how to use the round() statement to get it to display the Celsius temp to one place after the decimal point.

Link to comment
Share on other sites

Well, you're not trying to use it anywhere so I don't know what to tell you about why it's not working...

 

round

float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )

Returns the rounded value of val to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).

Link to comment
Share on other sites

$cTemp is the variable holding the Celsius value. You can round it in two places:

1. When you calculate it

2. When you output it

 

#2 is the best choice: leave the math be as exact as possible and make it only show the one decimal place. That idea of not modifying a value early on and instead waiting until you need to do so (and to do it without permanently modifying the original value) is a good habit to get into as it will help you later with other concepts.

 

Instead of outputting $cTemp, output the result of the round() function; "$val" is the value and "$precision" is the number of decimal places to round to.

round($cTemp, 1)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.