I am building a small price calculator in the Product type entry for Craft Commerce by building a custom plugin. This plugin executes my javascript file.

The issue I am encountering is that I need to do calculations with the numbers fields that are present in the form. Since they are not real number fields users are allowed to use both comma's and points as separator for decimals.

So my issue is that I never know if the comma or the point is the decimal separator.

What could fix this issue? Can I force Craft to use real number fields?

1 Answer 1


Craft's number fields aren't represented by strings. You can check that by putting a debug statement like this in one of your templates:

{% dd product.myNumberField %}

If you get something like '3.141', that's a string. 3 is an integer and 3.141 is a float.

For number fields, this will give you an integer or float, depending on whether the field value is an integer or a fraction.

If you see both commas and dots in the number field in the CMS, that's the local input format at play. The value will be interpreted based on the user's Formatting Locale set in their account preferences. For example, if you set this to English, commas are treated as thousands separator, and the dot as radix/decimal separator. If you set it to German, you get the inverse behaviour (we're weird that way …)

If you switch between languages in your developer account, it takes some getting used to. But your authors just need to set their account to their preferred language and remember to use the convention of that language, then it will work fine.

  • My issue is that the fields themselves are text fields with an attribute inputmode="numeric". I want to do calculations with JS by reading the value of the fields. That means I can't use the Craft helpers to translate text value to the real number. With a number field this is not an issue since you always get the correct number formatting for calculations in JS. With the text fields in Craft I need to manually convert the comma's to dots in order to be able to do some calculations.
    – Matthias
    Feb 12 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.