Skip to content
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

Create format_number filter #999

Merged
merged 6 commits into from
Feb 6, 2023
Merged

Create format_number filter #999

merged 6 commits into from
Feb 6, 2023

Conversation

julia-uy
Copy link
Contributor

@julia-uy julia-uy commented Jan 27, 2023

Since locales render numbers differently, there needs to be a way to format numbers based on the locale. Currently, the only way is to use the format_currency filter and get rid of the currency symbol.

This PR creates a format_number filter that will do this formatting natively.

format_number(locale, decimalPrecisionNumber) where locale is String of an ISO language code and maxDecimalPrecision is a number that determines the number of decimal digits of the formatted input value.

Examples:

  • {{ 1000|format_number('en-US') }} ==> 1,000

  • {{ 1000.333|format_number('en-US') }} ==> 1,000.333

  • {{ 1000.333|format_number('en-US', 2) }} ==> 1,000.33

  • {{ 1000|format_number('fr') }} ==> 1 000

  • {{ 1000.333|format_number('fr') }} ==> 1 000,333

  • {{ 1000.333|format_number('fr', 2) }} ==> 1 000,33

  • {{ 1000|format_number('es') }} ==> 1.000

  • {{ 1000.333|format_number('es') }} ==> 1.000,333

  • {{ 1000.333|format_number('es', 2) }} ==> 1.000,33

@julia-uy julia-uy marked this pull request as ready for review January 27, 2023 19:59
@julia-uy julia-uy requested review from tkindy and AlpriElse January 27, 2023 19:59
Copy link
Contributor

@tkindy tkindy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly just a couple naming things, but generally LGTM

private String formatNumber(
Locale locale,
BigDecimal number,
int noOfDecimalPlacesInInput,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this parameter's value comes from number, I think we can get it inside this method instead of passing it as a separate parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! In the main method, I was using this variable as a fallback to the max decimal precision variable if it wasn't passed in, but I ended up switching the max decimal precision variable to an Optional so it no longer needs to be a parameter for this method

) {
NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);

numberFormat.setMinimumFractionDigits(noOfDecimalPlacesInInput);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to set this, actually? I'd think the default NumberFormat would try to show the entire passed-in value by default, which would mean it would use all decimal digits (unless it was over the maximum).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked with the unit tests I wrote and I think you are correct. This doesn't need to be set since by default - it'll just output all the decimal digits unless a maximum less than the number of decimal digits was set

@JinjavaSnippet(code = "{{ number|format_number(\"en-US\", 3) }}")
}
)
public class NumberFormatFilter implements Filter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think FormatNumberFilter would align with how filters are named relative to their representation in Jinjava, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed 👍🏼

@julia-uy julia-uy requested a review from tkindy February 6, 2023 20:25
@@ -82,6 +82,7 @@ protected void registerDefaults() {
Md5Filter.class,
MinusTimeFilter.class,
MultiplyFilter.class,
FormatNumberFilter.class,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go higher up to maintain the alphabetic sort now that the name has changed.

@julia-uy julia-uy merged commit 91a3f0e into master Feb 6, 2023
@julia-uy julia-uy deleted the format-number-filter branch February 6, 2023 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants