journalize icon indicating copy to clipboard operation
journalize copied to clipboard

intcomma rounding

Open palewire opened this issue 4 years ago • 1 comments

I don't know if I would call this a feature or a bug or really anything more than just a thing, but I've noticed that intcomma decides to round off decimals of zero, but not other numbers. For instance:

journalize.intcomma(1000.0)
'1,000'

This is the case even when the user submits a value with an explicitly fixed decimal place.

journalize.intcomma((1000.0).toFixed(1))
'1,000'

I'm certain there is probably some good reason for this I'm unaware of, but I wanted to file this ticket since the second example above was not the behavior I expected as a user.

For what it's worth, it is also not the behavior of the old gods of Django.

>>> from django.contrib.humanize.templatetags.humanize import intcomma
>>> intcomma(1000.0)
'1000.0'

palewire avatar Jul 02 '20 22:07 palewire

Huh! I think that's a legitimate bug. The issue crops up because the very first thing intcomma does is coerce the input to a number:

https://github.com/rdmurphy/journalize/blob/d398d3570e9525d5fa4c784c310ebb4264c697f8/src/intcomma.js#L38

So if it comes over as a string ("1000.0") it's gonna get pulled off. But this may really be a legitimate difference between how Python and JavaScript works. In Python declaring a value as 1000.0 will respect the trailing 0, but JavaScript will not.

Python

val = 1000.0
print(val)
# 1000.0

JavaScript

const val = 1000.0;
console.log(val);
// 1000

So I think the question is should intcomma respect and mirror what comes after a decimal with a string input, and is there anything to be done for number inputs?

rdmurphy avatar Jul 02 '20 23:07 rdmurphy