New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function: Format number with locale #206

Closed
djmj opened this Issue Feb 6, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@djmj

djmj commented Feb 6, 2016

The provided number formatting functions assume the data shall always be formatted using view locale.

This is not always the case. For some javascript or SEO parameters I need US format of a number.

#{of:formatNumberLocale(bean.number, pattern, Locale.US)}
#{of:formatNumberDefaultLocale(bean.number, 'en-us')}

/**
 * Format the given number in the given locale-default pattern. This is useful when you want to format numbers in
 * for
 * example the <code>title</code> attribute of an UI component, or the <code>itemLabel</code> attribute of select
 * item, or wherever you can't use the <code>&lt;f:convertNumber&gt;</code> tag. If no format locale is provided it
 * will be set to the one as obtained by {@link Faces#getLocale()}.
 *
 * @param number The number to be formatted in the given locale-default pattern.
 * @param locale Optional {@link Locale} instance or a String that can be evaluated to one.
 *
 * @return The number which is formatted in the given locale-default pattern.
 */
public static String formatNumberDefaultLocale(final Number number, final Object locale)
{
    if (number == null)
        return null;

    return NumberFormat.getNumberInstance(ElUtils.getLocale(locale)).format(number);
}

/**
 * If no locale is provided the one as obtained by {@link Faces#getLocale()} is returned.
 *
 * @param locale Optional {@link Locale} instance or a String that can be evaluated to one.
 *
 * @return
 */
public static Locale getLocale(final Object locale)
{
    if (locale == null)
        return Faces.getLocale();
    else if (locale instanceof Locale)
        return (Locale) locale;
    else
        return new Locale(String.valueOf(locale));
}

Add

Since these formatting is used for 'hidden' numbers, such as in javascript calls, I always want to render as many fractional digits as possible so I created an el function formatNumberFraction(final Number number) which does exactly this without providing custom pattern.
Default NumberFormat renders just three maximum fractional digits and i am too lazy to provide a format always.
An alternative for omnifaces could be a method like formatNumberFraction(number, maxFractionalDigits).

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Feb 6, 2016

Member

Providing locale makes sense.

Member

BalusC commented Feb 6, 2016

Providing locale makes sense.

@BalusC BalusC closed this in 29c8156 Feb 6, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment