numbro
numbro copied to clipboard
en-IN localization and customizable grouping
Helps to address the grouping issues in #2 and #235
Added support for customizable grouping widths. In the delimiters, specify group: [2,3], then format() will group like this
12,00,00,000
To make this work, we build up the grouping regex according to the group configuration. For en-IN, we get this:
/(\d)(?=(?:\d{2})*(?:\d{3})(?!\d))/
It matches a single digit, followed by non capturing look-ahead that matches zero or more 2 digit groups, followed by a 3 digit group.
The default for the group property is [3] which matches the current behaviour of grouping by thousands.
It should be possible to pass [4] and get grouping by ten thousands for e.g. Japanese. I'm not aware of more complex groupings.
I should point out that I don't have confidence in the abbreviations, ordinal, currencyFormat and the other formats (e.g. fourDigits). I didn't really know what they should be, but I wanted to do the customizable grouping part - someone with experience in number formatting in India might have suggestions.
letting this one open a bit more to see if someone has an opinion. Ok with you?
@BenjaminVanRyseghem yes, if you could add the help wanted tag that would probably by a good idea.
I'm just learning about this project, and was glad to see that it has gotten a lot further than it was in numeral.js. One big thing I was looking for is whether this issue (along with the Japanese issues mentioned in the links above) are being addressed.
This appears to still be open. I do work with a couple of people who would be able to answer questions about number formats in Tamil and Japanese. So, if there are questions that would cause the changes for those languages to be incorporated, I'll be glad to get them answered.
There is now the option delimiters.thousandsSize
that is language specific. It should cover those cases :smile:
If someone is willing to test and report :smile:
Are the localization files going to be added/updated? Because if not, it will take me a fair amount of time to become familiar enough with the code to do that.
to do that.
To do what?
Be the someone who is willing to test and report.
numbro.language(); //> "en-US"
numbro.languageData().delimiters.thousandsSize = 2;
numbro(100000).format(","); //> 10,00,00
@BenjaminVanRyseghem, solution above does not solve the case completelly. In Indian system
- 100000 (one hundred thousand) would be
1,00,000
; - 10000000 (ten million) would be
1,00,00,000
.
Do you see the pattern? The very first thousand is delimited by 3 chars, then delimiter comes every 2 chars, following from right to the left.
Apparently, thousandsSize
is not that kind of a property in setup; rather it should be something like delimiterGroups
property, but I cannot figure out a design for that, at the moment.
Maybe:
delimiterGroups: [2, 3] // default [2]
I see! I will see if I can add a function like this:
thousandsSize = 2; //> 10,00,00
thousandsSize = (input) => {
let length = input.length;
if (length <= 5) {
return 2;
}
let rest = length - 2;
let result = [];
for (let i = 0; i < Math.floor(rest/3), i++) {
result.push(3);
}
result.push(2);
return result;
} //> 100,000,00
The number you have in the final comment is backwards. It is every 2 places, except for the very last one which is 3 places. You have it the other way around. 100 1,000 10,000 1,00,000 10,00,000 1,00,00,000
When will the en-IN be merged. We are in need of this urgently.
@BenjaminVanRyseghem is there any intention of ever merging this code?
this PR is quite out-dated, has conflicts, and I have no idea about how en-IN should work.
So if someone with indian's cardinal knowledge is willing to take care of this, I will definitely push for it. But in the current situation, there is sadly not much I can do