Math with numeric values derived from a dropdown field
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
add a comment |
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
add a comment |
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
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
drop-down number math
edited Feb 1 at 18:23
Jonathan Schofield
asked Feb 1 at 18:16
Jonathan SchofieldJonathan Schofield
1516
1516
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Have you tried subhead.hLevel.value
?
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Have you tried subhead.hLevel.value
?
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
add a comment |
Have you tried subhead.hLevel.value
?
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
add a comment |
Have you tried subhead.hLevel.value
?
Have you tried subhead.hLevel.value
?
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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