String being coerced to a number

I’ve a string that’s a short record locator ID in document front matter (for example “F7BN” or “4E12”). I want to render these out per page, but while the first one works fine the second is being cast to a large number because it’s being read as exponential notation. The only way I’ve got so far of “fixing” this is quoting the ID in the front matter (e.g. id: "4E12" instead of id: 4E12) which is fine but feels inelegant.

I’ve tried things like:

{{(dict "locator" "4E89") | jsonify}}
{{ string 4E89 }}

and even weirder things like:

{{ substr 4E89 1 2 }}

to no avail…
Is there a way to have hugo alway treat the field as a string?

Using print perhaps?

It’s necessary to quote the special string like 4E89, otherwise it will be treated as a number.

See also Scientific notation - Wikipedia.

Yeah, I was wondering if there’s a way to avoid that - thanks though razon.

What’s the use case? That is, why is quoting the string, which happens to also be a number, a problem?

The ID is set by a python script, and the python yaml parser just sees a string and dumps it without quotes. Not a problem per se, now I know this is the behaviour I can explicitly quote the string, but will need to check through a few hundred records for unquoted strings-that-happen-to-be-a-number. Was hoping I could guard against this on the Hugo side as well as fixing the python end.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.