Author Topic: Rounding temperature decimal on ajax script  (Read 958 times)

0 Members and 1 Guest are viewing this topic.

Offline ValentineWeather

  • Forecaster
  • *****
  • Posts: 6364
    • Valentine Nebraska's Real-Time Weather
Rounding temperature decimal on ajax script
« on: September 21, 2018, 01:04:12 PM »
Is there anyway to round decimal to nearest whole number. Only (Temperature) I know it's a weird request, the local radio station uses the station on air and instead of teaching everyone how it works with multiple DJ's I thought I would first look for a work around on my end.
Randy

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 947
    • LexMAWeather
Re: Rounding temperature decimal on ajax script
« Reply #1 on: September 21, 2018, 05:55:07 PM »
I'm not 100% sure I understand the question, but if you are just looking to round to the nearest integer, Math.round(<number>) is what you use.
https://lexmaweather.info
On Mastodon: @LexMAWeather@toot.community

Offline ValentineWeather

  • Forecaster
  • *****
  • Posts: 6364
    • Valentine Nebraska's Real-Time Weather
Re: Rounding temperature decimal on ajax script
« Reply #2 on: September 21, 2018, 07:01:20 PM »
I was looking to round to nearest whole number leaving decimal off temperature (f) . Just on ajax script. Example 54.4 would show 54 rounded for temperatures. I use cumulus so was looking in the ajaxCUwx.js
I see math round function being used but not sure exactly where or how it should be placed.
Randy

Offline ValentineWeather

  • Forecaster
  • *****
  • Posts: 6364
    • Valentine Nebraska's Real-Time Weather
Re: Rounding temperature decimal on ajax script
« Reply #3 on: September 21, 2018, 07:11:02 PM »
You would think anyone would understand to round temperature to nearest whole number but not the case. After further thought I've decided to leave decimal in place. If it's something easy to do I would still like to know however.
Thx 
Randy

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 947
    • LexMAWeather
Re: Rounding temperature decimal on ajax script
« Reply #4 on: September 21, 2018, 07:52:28 PM »
OK, so you can use Math.round() or alternately toFixed(). The difference is that the former returns an integer while the latter returns a string. So... if the variable "temp" has the temperature as a float,

Code: [Select]
var roundedInt = Math.round(temp);
var roundedString = temp.toFixed(0); // "0" means no decimal places

If you are going to display the result, use the second (toFixed) option.

There are examples of both functions all over the MB version of the ajax code so I assume the CU code has it also.  The MB code that formats the temperature so that there is one decimal place is:

Code: [Select]
    set_ajax_obs("ajaxtemp", temp.toFixed(1) + uomTemp);

The "1" as a parameter is what makes the one decimal point show up. If all you are trying to do is use no decimal places anywhere, just change that to zero. You'll also need to apply the same to the set_ajax_obs() calls that affect other instances of the temperature (e.g., "ajaxtempNoU", "gizmotemp"...).
https://lexmaweather.info
On Mastodon: @LexMAWeather@toot.community

Offline ValentineWeather

  • Forecaster
  • *****
  • Posts: 6364
    • Valentine Nebraska's Real-Time Weather
Re: Rounding temperature decimal on ajax script
« Reply #5 on: September 21, 2018, 08:55:57 PM »
Thanks appreciate the help. I've changed mind on doing this so going to leave as is but as a followup I did change all the 1's to 0 and made no difference.
I had already done this prior to post here and just tried again. Usually there is something else I'm missing anytime I make a change. ](*,)
I really don't want just removal of decimal but want it rounded correctly off if removed.  I see the big temperature was already zero and that's true, so I'm sure somewhere else it also needs changed.

 
        //Temperature
      var temp = convertTemp(realtime[2]);
      set_ajax_obs("ajaxtemp", temp.toFixed(1) + uomTemp);
      set_ajax_obs("ajaxtempNoU", temp.toFixed(1));
      set_ajax_obs("gizmotemp", temp.toFixed(1) + uomTemp);
      set_ajax_obs("ajaxbigtemp",temp.toFixed(0) + uomTemp);

Randy

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 947
    • LexMAWeather
Re: Rounding temperature decimal on ajax script
« Reply #6 on: September 21, 2018, 09:30:16 PM »
1) toFixed() will do rounding.

2) If you have changed the ajax code to the following:

Code: [Select]
      set_ajax_obs("ajaxtemp", temp.toFixed(0) + uomTemp);
      set_ajax_obs("ajaxtempNoU", temp.toFixed(0));
      set_ajax_obs("gizmotemp", temp.toFixed(0) + uomTemp);

I believe that is all you need to change in the JS file.

However, those fields are initially set in the PHP code. You need to find the instances of id="ajaxtemp" (ajax-dashboard.php), id="ajaxtempNoU" (software dependent trends code), and id="gizmotemp" (ajax-gizmo.php) in the code and modify that code. The PHP code just uses whatever the value is from the tags file, and I assume that's always with one decimal point.
https://lexmaweather.info
On Mastodon: @LexMAWeather@toot.community

Offline Jasiu

  • Forecaster
  • *****
  • Posts: 947
    • LexMAWeather
Re: Rounding temperature decimal on ajax script
« Reply #7 on: September 21, 2018, 09:37:31 PM »
Oh - and once you find those lines of PHP code, where you see $temperature used, substitute $tempnodp.
https://lexmaweather.info
On Mastodon: @LexMAWeather@toot.community

Offline ValentineWeather

  • Forecaster
  • *****
  • Posts: 6364
    • Valentine Nebraska's Real-Time Weather
Re: Rounding temperature decimal on ajax script
« Reply #8 on: September 21, 2018, 09:58:46 PM »
Will do thank you very much.  =D&gt;
Randy