Comments (18)
This is great! Makes me so happy. Thanks for adding this issue.
from d3-format.
I haven’t added it yet. Just copying a request from mbostock/d3. I’m not sure yet how best to implement this. It makes me wonder if locales should be specified as code rather than as declarative data, though that introduces different problems.
from d3-format.
Here's a first stab an an implementation http://bl.ocks.org/curran/70348c8af4c1959c1289a20ff0a63490
from d3-format.
How about notation like this? grouping: [<,2,3]
I failed to try to tamper with the code in the repo for the grouping here
If we encounter '<' as grouping[0] then we will cycle through only the next number. If there is no character then we will cycle through the whole list.
Is there any test that I can use to try this fix and not break other parts of d3-format ?
from d3-format.
The format function in the link http://bl.ocks.org/curran/70348c8af4c1959c1289a20ff0a63490 doesn't work for all cases. A modified version of that would help. Here it is.
function format(n){
var negative=false;
var str = String(n);
if (str[0]=="-"){
str=str.substring(1,str.length);
negative=true;
}
var arr = [];
var i = str.indexOf(".");
if(i == -1){
i = str.length-1;
}
else {
for(var j = str.length - 1; j > i; j--){
arr.push(str[j]);
}
arr.push(str[i]);
i=i-1;
}
for(var n = 0; i >= 0; i--, n++){
if(n > 2 && (n%2==1)){
arr.push(",");
}
arr.push(str[i]);
}
if(negative)
arr.push("-");
return arr.reverse().join("");
}
from d3-format.
@Harshit20051 Thank you for your fixes! I updated the original example with variations of your modifications to handle the "10000" and negative number cases correctly. Here's the current result:
Here's the function:
function format(n){
var negative = n < 0;
var str = negative ? String(-n) : String(n);
var arr = [];
var i = str.indexOf(".");
// Handle decimal points
if(i === -1){
i = str.length;
} else {
// Add the digits after the decimal point.
for(var j = str.length - 1; j > i; j--){
arr.push(str[j]);
}
arr.push(".");
}
i--;
// Insert commas.
for(var n = 0; i >= 0; i--, n++){
if(n > 2 && (n % 2 === 1)){
arr.push(",");
}
arr.push(str[i]);
}
if(negative){
arr.push("-");
}
return arr.reverse().join("");
}
Maybe it would make sense to have most locales defined as declarative data, but add a hook for also defining locales such as this one as code.
from d3-format.
It’s nice for the locales to be strictly declarative. It means they can be defined as JSON, and if you load them from another host, you’re not running arbitrary code. @rohithdaka’s suggestion of using a special string in the grouping array to allow cycling seems like a clever solution that could work.
The closest you can get right now is to just have a really long grouping
definition:
[3, 2, 2, 2, 2, 2, 2, 2, 2, 2]
If you want to support arbitrarily large numbers, then we could do something like this:
[3, 2, "*"]
Here the "*"
is a special signifier that can only appear as the last entry in the grouping definition, and it means to repeat the last group value indefinitely (rather than cycling). This is basically the mirror of @rohithdaka’s suggestion but I think it more closely matches the current definition.
I suppose it’d be nice to allow more arbitrary repeating sequences but without any other examples to go on, it’s hard to generalize. Trying to keep things simple.
from d3-format.
Thank you for taking a look at this issue. I have lots of locale json files (that belong to several languages in India), should I submit them with [3,2,"*"] or [3,2,2,2,2,2,2,2,2,2,2] ?
practically speaking a array of length 30 (One 3 and twenty-nine 2s) should be good as 10^62 is the highest number that was given a name in many Indian languages. Please advice.
from d3-format.
Well, in fact d3-format doesn’t currently support decimal notation values greater than or equal to 1e21; internally, we use number.toString(10) which behaves inconsistently with number.toString(2), number.toString(8) and number.toString(16).
I suppose we should fix that, but I think it would either require a hack (like using number.toString(20) and then expanding the result) or writing our own decimal formatter (which would be slow and possibly prone to rounding problems).
from d3-format.
Got it. I will submit with an array of length 10 (One 3 and Nine 2s), just like you suggested in the beginning. Thank you :)
from d3-format.
Sounds good!
from d3-format.
This is still unsolved?
Perhaps it would be worth creating a D3 plugin with the bit of code I made for the Indian number formatting? Seems a shame to special case this one, but would better to have a solution be an npm install
away, rather than a copy-paste job from https://bl.ocks.org/curran/70348c8af4c1959c1289a20ff0a63490
from d3-format.
OK, here's what I've got so far https://github.com/curran/d3-format-india
@mbostock Just give the word and I'll transfer it to github.com/d3/d3-format-india
from d3-format.
Here's an interesting regex-based solution by @vkbansal
https://vkbansal.me/blog/format-indian-currency-in-js/
(1234567.8).toFixed(2).replace(/(\d)(?=(\d{2})+\d\.)/g, '$1,') // "12,34,567.80"
from d3-format.
@curran why make this d3 specific. This can be used independent of d3.
from d3-format.
Right, the module could be used independently of D3. It is already like that. It's just set up as a "D3 Plugin" for convenient use within D3. That doesn't preclude using it as a standalone module, independent of D3.
from d3-format.
I think there was never a followup pull request, but it seems like this should work:
{
"decimal": ".",
"thousands": ",",
"grouping": [3, 2, 2, 2, 2, 2, 2, 2, 2, 2],
"currency": ["₹", ""]
}
Review #35?
from d3-format.
Sorry for writing on this closed issue.
But I'm trying to find information about the Indian digit groupings, because there seems to be two ways of doing Indian numbers.
One-word system:
thousand - lakh - crore - arab - kharab - nil - padma - shankh - ald - ank - jald - madh - parardh - ant - …
Each, except thousand, is a magnitude of 100, and represents ",00" in the formatting. So one shankh is 1,00,00,00,00,00,00,00,000.
[3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, …]
Lakh-crore system:
thousand - lakh - crore - thousand crore - lakh crore - crore crore - thousand crore crore - lakh crore crore - crore crore crore - …
Thousand is ",000" - lakh is ",00,000" - crore is ",00,00,000". So one lakh crore crore is "1,00,000,00,00,000,00,00,000"
[3, 2, 2, 3, 2, 2, 3, 2, 2, 3, 2, 2, 3, …]
Because I've seen numbers formatted in both systems at different places. The lakh-crore pattern is less common to find, but it fits the lakh-crore speech pattern.
I wonder if there's more information about this that I could read more about. This is a bit interesting to see.
—
And there's also a third system (lakh-arab system) counting up to arab instead of crore, but functions the same as the lakh-crore system.
One thousand arab arab is "1,000,00,00,00,000,00,00,00,000"
[3, 2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, …]
from d3-format.
Related Issues (20)
- import conflict with 3.0.0 update HOT 7
- Error with jest in version 3.0.0 HOT 1
- `"c"` format does not work as expected HOT 4
- Hot reload is complaining about named functions in nextjs HOT 4
- Using the locale .json files directly is not possible anymore HOT 1
- Add any string suffix to number. HOT 1
- Pick locale based on users browser automatically rather than fetching the locale definitions HOT 2
- Missing d3-format.min.js HOT 3
- Undefined Import in Next.js HOT 1
- I am using d3 in nextJs 7.0.x , it is giving error Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Home\Wildlife-Insights\node_modules\d3\package.json. HOT 1
- Using custom units in d3-format HOT 3
- Is it possible to support locale zh-TW? HOT 3
- formatPrefix will sometimes not perform rounding HOT 5
- Should functions be named across `d3-*` ? HOT 4
- Unit is generated outside parenthesis for negative numbers
- zh-TW is not supported HOT 2
- Specify type `g` decimal to exponential break point
- formatPrefix vs format handling of parentheses
- The minus sign generated by d3.format not compatible with SVG HOT 1
- d3 format transfer minus(-) to ascii 8722 code HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from d3-format.