What governs stored decimal point for number field?

Hello,

A number field shows a comma (,) decimal point, but actually stores a dot (.) decimal point, this is how it looks in the panel

image

If I use a dot instead of a comma, save and reload, the comma appears again.

This is the number field code:

  price:
    label: Price
    type: number
    step: ,01
    width: 1/3
    placeholder: 0,00
    after: €

I’ve set my locale in the config:

'locale' => [
	LC_ALL      => 'es_ES.utf8',
],

This is the output of <?php dump(localeconv()) ?>

Array
(
    [decimal_point] => ,
    [thousands_sep] => .
    [int_curr_symbol] => EUR 
    [currency_symbol] => €
    [mon_decimal_point] => ,
    [mon_thousands_sep] => .
    [positive_sign] => 
    [negative_sign] => -
    [int_frac_digits] => 2
    [frac_digits] => 2
    [p_cs_precedes] => 0
    [p_sep_by_space] => 1
    [n_cs_precedes] => 0
    [n_sep_by_space] => 1
    [p_sign_posn] => 1
    [n_sign_posn] => 1
    [grouping] => Array
        (
            [0] => 3
            [1] => 3
        )

    [mon_grouping] => Array
        (
            [0] => 3
            [1] => 3
        )
)

So, how do I get a comma in the content file, so when I echo the product’s price, I also get a comma instead of a dot ?

Interestingly if I change LC_ALL the output of localeconv() also changes, BUT it does not seem to change the behaviour of the number field in the panel. Even if I use LC_ALL => 'en_GB.utf8', the number field insists on using a comma.

Which leads me to believe this is governed by something else than localeconv() ?

Thank you

Kirby rounds the value, despite what is stored in the content field. “7.50” wil become “7.5” and “7.00” will become “7”.

To get around this in a project where i also needed to display prices, I ended up making a field method to reformat the number.

'fieldMethods' => [
  'price' => function ($field) {
    return number_format((float)$field->value, 2, '.', '');
  }
]

Store it with a dot, and change the dot to a comma in the method and you should get what you want :slight_smile:

Im not sure how to fix the storage glitch though.

2 Likes

I think there is no way to change what is stored in the content file. It probably makes sense to store a “universal” value, then convert to format you need in your template. You would have to do that anyway if you want to support different languages.

Thank you both.

So there is no Kirby specific method to handle locale conversions for decimal , thousands, etc points?

…in snippets and templates I mean

Using $page->field()->toFloat() casts the field value to float and should respect the locale. However, given that zero value decimals are removed from the field value, using number_format() is required to make sure you get the number of decimals you want.

@texnixe, while your answer to this post solved my problem, it did not really answered the question in tht title, and I just came back to it:

What governs the decimal and thousands separator used in the number field in the panel ?

Changing the Locale in the config file seems to change the decimal separator used when echoing the value into the page, but it does not change what is shown to me on the number field, which is always a comma (for this site, on localhost, at least).

So what determines it ?

Thank you

What is shown in the number field, seems to depend on the browser language… at least in my baby test changing the language in Firefox from German to English changed the decimal comma to a dot.

You might get a different result on Windows. Some windows browsers (Internet Explorer and Edge i think) get the locale from the operating system by default, rather then the browsers set language. It can be overridden in the browsers settings but that requires user intervention.

Safari on a mac also does this, atleast i cant see any language settings in the Safari preferences.

Thank you both, that clears it out generally.

Hey,

I have a similar problem. Instead of the comma, I get a dot for decimal numbers.
I set the locale to de_DE.utf-8
Tested it in Safari, Chrome and Firefox, all german language active

So whats wrong? How do I get the comma instead of the dot?

This is in my blueprint:

          productPrice:
            type: number
            label: Preis
            before: €
            step: ,01
            placeholder: 0,00

Thats the template

<p><?= $data->productPrice()->toFloat() ?> €</p>

and thats in the content file

Productprice: 23.23