Math with numeric values derived from a dropdown field












2















I had a dropdown field with a handle of hLevel, with only numeric values in it, anticipating that I could do math with the values in my template. But it seems I can't.



{% set dif = subhead.hLevel - subhead.next.hLevel %}



…returns an error message ending in:




“could not be converted to int”




I found a thread convinced that number_format “converts a string to a number”, so I tried that:



{% set dif = subhead.hLevel|number_format - subhead.next.hLevel|number_format %}



…but that doesn't work either. The error message changes to:




“could not be converted to double”




Which doesn't surprise me as I did think that number_format is for float to formatted string conversion, not the other way round as well. ¯_(ツ)_/¯



When I change the field format to number everything works fine, but…



It would be good if there is an option to have dropdown field values be parsed as numbers, because from a UX perspective I would prefer that the field is a dropdown.



Or is there some workaround I've missed?










share|improve this question





























    2















    I had a dropdown field with a handle of hLevel, with only numeric values in it, anticipating that I could do math with the values in my template. But it seems I can't.



    {% set dif = subhead.hLevel - subhead.next.hLevel %}



    …returns an error message ending in:




    “could not be converted to int”




    I found a thread convinced that number_format “converts a string to a number”, so I tried that:



    {% set dif = subhead.hLevel|number_format - subhead.next.hLevel|number_format %}



    …but that doesn't work either. The error message changes to:




    “could not be converted to double”




    Which doesn't surprise me as I did think that number_format is for float to formatted string conversion, not the other way round as well. ¯_(ツ)_/¯



    When I change the field format to number everything works fine, but…



    It would be good if there is an option to have dropdown field values be parsed as numbers, because from a UX perspective I would prefer that the field is a dropdown.



    Or is there some workaround I've missed?










    share|improve this question



























      2












      2








      2








      I had a dropdown field with a handle of hLevel, with only numeric values in it, anticipating that I could do math with the values in my template. But it seems I can't.



      {% set dif = subhead.hLevel - subhead.next.hLevel %}



      …returns an error message ending in:




      “could not be converted to int”




      I found a thread convinced that number_format “converts a string to a number”, so I tried that:



      {% set dif = subhead.hLevel|number_format - subhead.next.hLevel|number_format %}



      …but that doesn't work either. The error message changes to:




      “could not be converted to double”




      Which doesn't surprise me as I did think that number_format is for float to formatted string conversion, not the other way round as well. ¯_(ツ)_/¯



      When I change the field format to number everything works fine, but…



      It would be good if there is an option to have dropdown field values be parsed as numbers, because from a UX perspective I would prefer that the field is a dropdown.



      Or is there some workaround I've missed?










      share|improve this question
















      I had a dropdown field with a handle of hLevel, with only numeric values in it, anticipating that I could do math with the values in my template. But it seems I can't.



      {% set dif = subhead.hLevel - subhead.next.hLevel %}



      …returns an error message ending in:




      “could not be converted to int”




      I found a thread convinced that number_format “converts a string to a number”, so I tried that:



      {% set dif = subhead.hLevel|number_format - subhead.next.hLevel|number_format %}



      …but that doesn't work either. The error message changes to:




      “could not be converted to double”




      Which doesn't surprise me as I did think that number_format is for float to formatted string conversion, not the other way round as well. ¯_(ツ)_/¯



      When I change the field format to number everything works fine, but…



      It would be good if there is an option to have dropdown field values be parsed as numbers, because from a UX perspective I would prefer that the field is a dropdown.



      Or is there some workaround I've missed?







      drop-down number math






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 1 at 18:23







      Jonathan Schofield

















      asked Feb 1 at 18:16









      Jonathan SchofieldJonathan Schofield

      1516




      1516






















          1 Answer
          1






          active

          oldest

          votes


















          4














          Have you tried subhead.hLevel.value?






          share|improve this answer
























          • Doh! I am an idiot. That works. Thanks Olivier :)

            – Jonathan Schofield
            Feb 1 at 18:26






          • 1





            @JonathanSchofield Happened to me, that's the only reason I picked up on it ;) Glad I could help

            – Olivier Bon
            Feb 1 at 18:30











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "563"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcraftcms.stackexchange.com%2fquestions%2f29352%2fmath-with-numeric-values-derived-from-a-dropdown-field%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Have you tried subhead.hLevel.value?






          share|improve this answer
























          • Doh! I am an idiot. That works. Thanks Olivier :)

            – Jonathan Schofield
            Feb 1 at 18:26






          • 1





            @JonathanSchofield Happened to me, that's the only reason I picked up on it ;) Glad I could help

            – Olivier Bon
            Feb 1 at 18:30
















          4














          Have you tried subhead.hLevel.value?






          share|improve this answer
























          • Doh! I am an idiot. That works. Thanks Olivier :)

            – Jonathan Schofield
            Feb 1 at 18:26






          • 1





            @JonathanSchofield Happened to me, that's the only reason I picked up on it ;) Glad I could help

            – Olivier Bon
            Feb 1 at 18:30














          4












          4








          4







          Have you tried subhead.hLevel.value?






          share|improve this answer













          Have you tried subhead.hLevel.value?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 1 at 18:21









          Olivier BonOlivier Bon

          64437




          64437













          • Doh! I am an idiot. That works. Thanks Olivier :)

            – Jonathan Schofield
            Feb 1 at 18:26






          • 1





            @JonathanSchofield Happened to me, that's the only reason I picked up on it ;) Glad I could help

            – Olivier Bon
            Feb 1 at 18:30



















          • Doh! I am an idiot. That works. Thanks Olivier :)

            – Jonathan Schofield
            Feb 1 at 18:26






          • 1





            @JonathanSchofield Happened to me, that's the only reason I picked up on it ;) Glad I could help

            – Olivier Bon
            Feb 1 at 18:30

















          Doh! I am an idiot. That works. Thanks Olivier :)

          – Jonathan Schofield
          Feb 1 at 18:26





          Doh! I am an idiot. That works. Thanks Olivier :)

          – Jonathan Schofield
          Feb 1 at 18:26




          1




          1





          @JonathanSchofield Happened to me, that's the only reason I picked up on it ;) Glad I could help

          – Olivier Bon
          Feb 1 at 18:30





          @JonathanSchofield Happened to me, that's the only reason I picked up on it ;) Glad I could help

          – Olivier Bon
          Feb 1 at 18:30


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Craft CMS Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcraftcms.stackexchange.com%2fquestions%2f29352%2fmath-with-numeric-values-derived-from-a-dropdown-field%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?