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

feat: format currency with commas and strip unnecessary decimal 0s #1120

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jhult
Copy link
Member

@jhult jhult commented Oct 14, 2024

Ensure commas where they belong and remove any trailing 0s after the decimal.

@jhult jhult marked this pull request as ready for review October 14, 2024 12:51
@@ -445,6 +445,64 @@ pub fn decorate_with_currency_tag(
.into()
}

/// Format currency by stripping out the decimal if it is 0 and calling
/// [format_with_commas].
pub fn strip_decimal_if_zero(num_str: String) -> String {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is really tricky. You have to take into account localization (different kinds of number formatting). Fortunately I have already developed a function for this:

pub fn normalize_number_format(number: &str) -> Result<String, String> {
. Could you use this function internally to first normalize the number across all locales and then strip the zero?

For context, this is how numbers are represented in German locale:

Screen Shot 2024-10-15 at 8 43 59 AM

Using the normalize function first will make a standard number (see unit tests on how), and then you can go from there.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You could also update how this function works:

fn test_trailing_zeros_dot() {

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