Question

i would like to show in the client the string 1,000 where the acutall value is int x = 1000 as integer how can I achive this ?

I am trying to do it by genrating new class myTextFiels which would have

@Override
public <C> IConverter<C> getConverter(Class<C> type) {
    IConverter<C> converter = super.getConverter(type);
    if (converter instanceof AbstractDecimalConverter<?>) {

        ((AbstractDecimalConverter<?>) converter).getNumberFormat(getLocale()).setGroupingUsed(true);
    } else {
        ((AbstractIntegerConverter<?>) converter).getNumberFormat(getLocale()).setGroupingUsed(true);
    }

    return converter;
}

but this does not seem to work , what am i missing

Was it helpful?

Solution

When you do getNumberFormat() on the converter, it doesn't return a reference to the NumberFormat instance that the converter will use, but rather a clone of the instance:

@Override
public NumberFormat getNumberFormat(final Locale locale)
{
    NumberFormat numberFormat = numberFormats.get(locale);
    if (numberFormat == null)
    {
        numberFormat = newNumberFormat(locale);
        setNumberFormat(locale, numberFormat);
    }
    return (NumberFormat)numberFormat.clone();
}

This is taken directly from the code of AbstractDecimalConverter. So to achieve what you want you should in your code do the following:

AbstractDecimalConverter castConverter = (AbstractDecimalConverter<?>) converter;
NumberFormat newNumberFormatForLocale = castConverter.getNumberFormat(getLocale());
newNumberFormatForLocale.setGroupingUsed(true);
castConverter.setNumberFormat(getLocale(), newNumberFormatForLocale);

However there's something you need to understand when you're doing this: the converter you get from the super.getConverter is retrieved using the Application's global converter locator. By default, the converter locator contains a singleton converter for every type.

What this means is that if you change this format at some point in one of your pages this way, then all of the other pages for that locale will use that formatting as well. I would imagine that this could be okay potentially, but if it isn't then instead of changing the NumberFormat on the converter you retrieve from the super.getConverter() I would create a new instance of the AbstractDecimalConverter in the getConverter method with the required NumberFormat set on it and return that instead, in which case the global singleton remains intact.

Do reply if you want anything elaborated at all.

OTHER TIPS

Perhaps not the prettiest solution but here's a bit of code to solve it

public static String numberformatter(int n){
    String number = "";

    int leftpart = n / 1000;
    int rightpart = n - (leftpart * 1000);
    number = rightpart + number;

    while(leftpart > 0){
        int old = leftpart;
        leftpart = old / 1000;
        rightpart = old - (leftpart * 1000);
        number = rightpart + "," + number;
    }

    return number;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top