Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Rounding numbers in Text_List #815

Closed
jsbrich opened this issue Jan 31, 2022 · 1 comment
Closed

Rounding numbers in Text_List #815

jsbrich opened this issue Jan 31, 2022 · 1 comment

Comments

@jsbrich
Copy link

jsbrich commented Jan 31, 2022

I am trying to round some temperature and humidity values to the nearest integer and I can't get it to work right.

  width: 1.1,
  height: 1.25,
  type: TYPES.TEXT_LIST,
  id: {}, // using empty object for an unknown id
  state: false, // disable state element
  list: [
  {
    title: 'Outside',
    icon: 'mdi-thermometer',
    value: '&sensor.outside_temperature.state',
    unit: '°F',
    filter: function(value) {
        var num = parseFloat(value);
        return num && !isNaN(num) ? num.toFixed(0) : value;
     }
 },

I would expect it to print something like Outside 35°F, but instead it is printing Outside 35.2°F

What am I missing?

@jsbrich
Copy link
Author

jsbrich commented Feb 1, 2022

Finally figured it out, so will post here in case anyone else is struggling.

I swear I tried this before posting, but this worked for me

                            position: [0, 0],
                            width: 1.1,
                            height: 1.25,
                            type: TYPES.TEXT_LIST,
                            id: {}, // using empty object for an unknown id
                            state: false, // disable state element
                            list: [
                                {
                                    title: 'Outside',
                                    icon: 'mdi-thermometer',
                                    unit: '°F',
                                    value: function() {
                                        var num = parseFloat(this.parseFieldValue('&sensor.outside_temperature.state'));
                                        return !isNaN(num) ? num.toFixed(0) : 'b';
                                    },
                                },

All the examples I saw, left the parseFloat call out and the toFixed was stumbling over parseFieldValue returning a string.

@jsbrich jsbrich closed this as completed Feb 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant