Add Decimal Place to Whole Number

I have a json string that I’m working with (just for learning purposes at the moment). Ive pulled in data from YouTube’s API and want to display 16000000 as 16.0M. This is what I have, but obviously it’s not the correct format I’m looking for.

{## Imitate available variables: ##}
{% set value_json = {
 "kind": "youtube#channelListResponse",
  "etag": "apfia4blXOrLYhFJeAz1bhfPFu4",
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 5
  },
  "items": [
    {
      "kind": "youtube#channel",
      "etag": "qei19gKsV47obGcRHna7K8Rq2JQ",
      "id": "UCBJycsmduvYEL83R_U4JriQ",
      "statistics": {
        "viewCount": "3102295352",
        "subscriberCount": "16000000",
        "hiddenSubscriberCount": false,
        "videoCount": "1458"
      }
    }
  ]

} %}

{{ value_json["items"][0].statistics.subscriberCount | int / 100000 | round(2) }}

You were only rounding the 100000, add some brackets.

By including that M in the state you will no longer be able to graph the state. If that is ok with you then this is the template you want:

{{ ( value_json["items"][0].statistics.subscriberCount | int(0) / 100000 )| round(1)  ~ 'M' }}

Otherwise leave out the ~ 'M' and add it as a unit_of_measurement instead.

1 Like

Thank you! That worked and definitely a learning moment. I did remove the M from the template just in case I ever graph it in the future. This was just a learning experience and something I may implement with a real ‘sensor’ soon. Thanks again!

1 Like