 # Rounding Values?

Hi there,

This is the code, I’m working on. I’m trying to make the Wind Chill value round to 0.1C. At the moment, I’m only getting either 2C or 0.0C, nothing else.

``\$chillC = round((\$chillF - 32) / 1.8);``

How does this work?

Thanks,

William

Hi there,

This is the code, I’m working on. I’m trying to make the Wind Chill value round to 0.1C. At the moment, I’m only getting either 2C or 0.0C, nothing else.

``\$chillC = round((\$chillF - 32) / 1.8);``

Thanks,

William
[/quote]

whats the value of \$chillf??
anyway the code will be something like this
[php]<?php

\$chillF = 38;
\$chillC = round((\$chillF - 3.2) / 1.8,1);
echo \$chillC;

?>[/php]
hope it helps

William

The PHP function round() can have up to three parameters:

``````round(value, prec, mode)

value = the number to be rounded
prec =	the number of decimal places to be rounded to. If this is omitted, prec defaults to 0 (zero)
mode =	can change how the rounding is done
``````

See www.php.net/manual/en/function.round.php for the full definition.

Therefore if you want to round to 0.1C you must specify prec. as 1. This will round to the nearest 1 decimal place.

[php]\$chillC = round((\$chillF - 32) / 1.8, 1);[/php]

Examples are:
Testing of round()
round(5.53) gives: 6
round(5.56) gives: 6
round(5.53,0) gives: 6
round(5.56,0) gives: 6
round(5.53,1) gives: 5.5
round(5.56,1) gives: 5.6
round(5.9856,2) gives: 5.99
round(5.9834,2) gives: 5.98

Hope this helps
Graham

WOW! That worked! So simple as that! Well, I’ll defenitely remember that, in future! Thanks so much, Graham! WOW! That worked! So simple as that! Well, I’ll definitely remember that, in future! Thanks so much, cogga28! Ok, now I’ve got 5.2C, there is now 5.0C which shows as 5C. Is it possible, to keep the 5.2C as it is but make the 5C, 5.0C?

What do I do, guys? Do I use the same formula as cogga28 and Graham, told me? :-\

And…what part of that page am I supposed to be look at? I’ve had a look at the page, bearing in mind the string ‘\$chillF’ changes all the time, because it is being picked up from a METAR station. So, I don’t see how this going to work! :’(

Ok, I’ve had a very good look through the page you gave me.

This is the code I have produced, so far:

``\$chillC = number_format(\$chillF, 1, '.', '');``

But, when I do that every value goes to 5C! >:(

Show me the full code.

[php]
\$num = 5;
echo number_format(\$num, 1); // output is 5.0
[/php]

Hi m@tt,

Here is the full code of Wind Chill:

[code]function mtr_get_wind_chill(\$tempF)
{
global \$lang,\$Debug, \$mtrInfo, \$metarPtr, \$group,\$UOMS;

``````// Calculate Wind Chill Temperature based on temperature in F and
// wind speed in miles per hour
if (isset(\$mtrInfo['WINDMPH']) and \$tempF < 51 && \$mtrInfo['WINDMPH'] !== 'calm')
{
\$pieces = explode(' ', \$mtrInfo['WINDMPH']);
\$windspeed = (integer) \$pieces; // wind speed must be in mph
if (\$windspeed > 3)
{
\$chillF = 35.74 + 0.6215 * \$tempF - 35.75 * pow(\$windspeed, 0.16) +
0.4275 * \$tempF * pow(\$windspeed, 0.16);
\$chillC = number_format(\$chillF, 1);
\$chillF = round(\$chillF);
\$chillC = round((\$chillF - 32) / 1.8, 1);
if(preg_match('|C|',\$UOMS['TEMP'])) {
\$mtrInfo['WIND CHILL'] = "\$chillC&deg;C";
} else {
\$mtrInfo['WIND CHILL'] = "\$chillF&deg;F (\$chillC&deg;C)";
}
}
}
``````

}
[/code]

Hope this helps…

William

Hi m@tt,

Is it possible, that I can round these bits of code, too?

[code]function mtr_get_temperature(\$part)
{
global \$lang, \$Debug, \$mtrInfo, \$metarPtr, \$group, \$UOMS;

if (preg_match(’/^(M?[0-9]{2})/(M?[0-9]{2}|[X]{2})?\$/’,\$part,\$pieces))
{
\$doMetric = preg_match(’|C|’,\$UOMS[‘TEMP’]);
\$tempC = (integer) strtr(\$pieces, ‘M’, ‘-’);
\$tempF = round(1.8 * \$tempC + 32);
{
if (!\$doMetric) {
\$mtrInfo[‘TEMP’] = \$tempF . “°F (” . \$tempC . “°C)”;
} else {
\$mtrInfo[‘TEMP’] = \$tempC . “°C”;
}[/code]

And:

```mtr_get_wind_chill(\$tempF); if (isset(\$pieces) and strlen(\$pieces) != 0 && \$pieces != 'XX') \$dewC = (integer) strtr(\$pieces, 'M', '-'); \$dewF = round((\$dewC + 32) + 1.8); if (!\$doMetric) { \$mtrInfo['DEWPT'] = \$dewF . "&deg;F (" . \$dewC . "&deg;C)"; } else { \$mtrInfo['DEWPT'] = \$dewC . "&deg;C"; } \$rh = round(100 * pow((112 - (0.1 * \$tempC) + \$dewC) / (112 + (0.9 * \$tempC)), 8));```

And:

```function mtr_get_altimeter(\$part) { global \$Debug, \$mtrInfo, \$metarPtr, \$group, \$UOMS; if (preg_match('/^(A|Q)([0-9]{4})/',\$part,\$pieces)) { if (\$pieces == 'A') { \$pressureIN = substr(\$pieces,2) . '.' . substr(\$pieces,2); // units are inches Hg, converts to hectoPascals \$pressureHPA = round(\$pressureIN / 0.02953,1); } else { \$pressureHPA = (integer) \$pieces; // units are hectoPascals \$pressureIN = Round(0.02953 * \$pressureHPA,2); // convert to inches Hg } if(preg_match('|inhg|i',\$UOMS['BARO'])) { \$mtrInfo['BAROMETER'] = "\$pressureIN inHg (\$pressureHPA hPa)"; } else { \$mtrInfo['BAROMETER'] = "\$pressureHPA hPa"; }```

Seriously guys, why have you given up on me? I have not sorted my issue! Please can someone help me? >:(

[php]
\$chillF = 35.74 + 0.6215 * \$tempF - 35.75 * pow(\$windspeed, 0.16) +
0.4275 * \$tempF * pow(\$windspeed, 0.16);
\$chillC = number_format(\$chillF, 1);
\$chillF = round(\$chillF);
\$chillC = round((\$chillF - 32) / 1.8, 1);
[/php]

You can’t format the number then round it again. This should be all you need.

[php]
\$chillF = 35.74 + 0.6215 * \$tempF - 35.75 * pow(\$windspeed, 0.16) +
0.4275 * \$tempF * pow(\$windspeed, 0.16);
\$chillC = number_format(round((\$chillF - 32) / 1.8, 1), 1);
[/php]

Hi m@tt,

Thanks for the code, I’m going to have to wait till the wind chill shows 5.0C before I can say this code works.

Also, is it possible to achieve this format in the other pieces of code?

Thanks,

William

Hi m@tt,

Thanks so much! The code you gave me, works! Also, the code you gave me, displays what I wanted! Thanks,

William