Make numpad '.' (dot) print ',' (comma) when entering decimal number in a field

Hi

I would like to mimic Excel behavior for French locale: when you press dot on the numpad it prints a comma, which is the genuine decimal separator.

Is there a way to intercept the key and convert the char on the fly during input ?

Alternatively I was thinking about overriding decimal datatypes by converting the dot into comma.

Regards
Michael

Hi Michael,

The simplest approach is to replace characters in the datatype. Below is an example of converting commas to dots for English locale, so it doesn’t matter if the user enters a dot or a comma, both will be used as a decimal separator.

Create a datatype:

package com.company.demo;

import com.haulmont.chile.core.datatypes.impl.BigDecimalDatatype;
import org.dom4j.Element;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Locale;

public class MyBigDecimalDatatype extends BigDecimalDatatype {

    public MyBigDecimalDatatype(Element element) {
        super(element);
    }

    @Override
    public BigDecimal parse(String value, Locale locale) throws ParseException {
        if (value != null) {
            value = value.replace(',', '.');
        }
        return super.parse(value, locale);
    }
}

Register it in metadata.xml with the same parameters as the original datatype in CUBA (keep format and separators as is because here they are used only for conversion without locale, so they are not relevant):

<metadata xmlns="http://schemas.haulmont.com/cuba/metadata.xsd">

    <datatypes>
        <datatype id="decimal" class="com.company.demo.MyBigDecimalDatatype"
                  default="true"
                  format="0.####" decimalSeparator="." groupingSeparator=""/>
    </datatypes>
    
...

Regards,
Konstantin

Hi Konstantin i have a similar problem but now if i use the dot of numpad it remain dot…i want comma instead…i have tried your example but i have too many bigdecimal datatype configured on my project…Is there another method to change the dot in comma?..i have read about overriding the bigdecimal dataype…how i can do it?
Regards

Hi Matteo,

The example above is precisely about overriding BigDecimalDatatype in a CUBA application.

If you are using Jmix, it’s even simpler: you don’t have to register your new class in XML, just annotate it as follows:

@DatatypeDef(id = "decimal", javaClass = BigDecimal.class, defaultForClass = true, 
        value = "demo_BigDecimalDatatype")
@NumberFormat(
        pattern = "0.####",
        decimalSeparator = ".",
        groupingSeparator = ""
)
public class MyBigDecimalDatatype extends BigDecimalDatatype {

    @Override
    public BigDecimal parse(String value, Locale locale) throws ParseException {
        if (value != null) {
            value = value.replace(',', '.');
        }
        return super.parse(value, locale);
    }
}

Your class will be used instead of the framework’s one because your application is lower in the hierarchy of add-ons.

thank you konstantin…no i’m on cuba 7…Do you mean that the solution that the February 2020 solution replaces the “.” without changing the datatype of the entity fields? I noticed that for some fields (with datatype bigdecimal) it works well, for others (equally bigdecimal) it works randomly… could it be a cache problem?

Yes I mean it should work for CUBA 7.2.

First make sure you are on the latest CUBA 7.2.20. If the problem persists, try to find differences between the fields where it works and where it doesn’t. Set a breakpoint to your MyBigDecimalDatatype code and debug it.

Thank you Konstantin. It’s working!