A tool that I am using is displaying a room temperature on a smart mirror. This line of code is creating the temperature value:
var temperatureTextWrapper = document.createTextNode(
zone.state.sensorDataPoints.insideTemperature.celsius + "°"
);
After this the var
is just being appended to an existing span
.
By default, the value contains two decimal places eg. 25.76°C
. However I would love to have it rounded to one decimal place or even full integers.
I already tried the .replace()
or .slice()
functions but with no success. What’s the best way to approach this?
2
Answers
You can use
Math.round()
to round to the nearest integer.In your case, you could use:
If you have access to this code, you can manipulate is using
toFixed
on the data itself:If you don’t you can find this element in the DOM (which will require some more information about the HTML to help you with) and then manipulate it:
See MDN Page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed