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

Percent converter ignoring % character. #387

Closed
djmj opened this Issue Jun 7, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@djmj

djmj commented Jun 7, 2017

JSF built in number converter requires a percentage number input including % character, which is just annoying for users to input the % sign.

I extended the built-in NumberConverter for this purpose. I was not sure if the % sign for all locales is the suffix. If yes the % could just be appended which makes this code even smaller.

What do you think?

Converter

/**
 * Basic {@link NumberConverter} which allows input of percentage numbers
 * without % character.
 */
@FacesConverter("package.NumberPercentConverter")
public class NumberPercentConverter extends NumberConverter
{
	private static final BigDecimal PERCENT_MULTIPLIER = new BigDecimal(0.01);

	@Override
	public Object getAsObject(final FacesContext context, final UIComponent component, String value)
	{
		// 20.5
		if (value == null)
			return null;

		// .5 -> 0.5
		value = MathUtils.prefixZero(value, this.getLocale());

		// check if % sign is missing
		if (this.getType().equals("percent") && !value.contains("%"))
		{
			this.setType("number");

			// 20.5
			Number number = (Number) super.getAsObject(context, component, value);

			// 0.205 - convert to percentage range
			number = BigDecimal.valueOf(number.doubleValue()).multiply(NumberPercentConverter.PERCENT_MULTIPLIER);

			this.setType("percent");

			// "20.5%" - format number using converter attributes and locale
			value = super.getAsString(context, component, number);
		}

		return super.getAsObject(context, component, value);
	}
}

Form

<h:form>
	<p:messages/>

	<p:inputText value="#{testBean.number}">
		<f:validateLongRange minimum="0" maximum="1"/>
		<o:converter converterId="de.store24.web.jsf.converter.NumberPercentConverter"
			maxIntegerDigits="3" maxFractionDigits="4" type="percent"/>
	</p:inputText>

	<p:commandButton value="Save" update="@form"/>
</h:form>

Prefix missing Zero

I also allowed input of .5 using regular expression to prefix a missing 0 before the decimal separator of that locale. Its just more user-friendly. I can provide this function if necessary.

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Dec 10, 2017

Member

Makes sense.

Member

BalusC commented Dec 10, 2017

Makes sense.

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Dec 10, 2017

Member

I added omnifaces.ImplicitNumberConverter for 3.0.

This not only covers percent formats but also currency formats.

Only prefixing with zero wasn't necessary in my case. It works just fine for me in Java 1.8 (Oracle JDK 1.8.0_111).

Thanks for improving OmniFaces with this suggestion!

Member

BalusC commented Dec 10, 2017

I added omnifaces.ImplicitNumberConverter for 3.0.

This not only covers percent formats but also currency formats.

Only prefixing with zero wasn't necessary in my case. It works just fine for me in Java 1.8 (Oracle JDK 1.8.0_111).

Thanks for improving OmniFaces with this suggestion!

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