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

Fix string to number short conversion #40

Closed
wants to merge 1 commit into from

Conversation

dshster
Copy link

@dshster dshster commented Dec 13, 2022

No description provided.

@@ -5,7 +5,7 @@ export default (num: number, {
formatThousands = false,
} = {}): string => {
/* string→number coercion after toFixed is for removing trailing zeros when they're existed: 17.00 → 17 */
const numStr = (+num.toFixed(2)).toString();
const numStr = (+num).toFixed(2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure that I've got the idea behind this change.

The comment above was saying what was going on here. The comment hasn't been changed, so I assume that there was an error, but not the line is fully “in accordance” with the comment.

Then, I'm testing the change:

const num = 17.0001;

console.log((+num.toFixed(2)).toString()); // '17'

console.log((+num).toFixed(2)); // '17.00'

The current implementation does not remove trailing zeros, while the comment says it should.

If the idea was to change the actual behavior, then it's better to add a flag or something, because there are users who rely on the current behavior. So it should not be changed, I believe.

Copy link

Choose a reason for hiding this comment

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

Judging by the README, it should be possible to pass a string: https://github.com/funbox/diamonds/blob/master/README.md#:~:text=Formats%20a%20number%20(-,or%20a%20string%20with%20a%20number%20inside,-)%20using%20the%20passed

Passing the string leads to an error, so I think we need to fix the README or change the behavior for string values only.

Copy link
Contributor

Choose a reason for hiding this comment

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

@leusrox, ah, I see.

I'd rather believe types, than README.

I think that this part in README was simply copy-pasted from formatPhoneNumberString description. But we are free to change the helpers to make them allow strings:

#41

Copy link

Choose a reason for hiding this comment

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

Thanks!

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.

None yet

3 participants