GithubHelp home page GithubHelp logo

Indian Number System format about d3-format HOT 18 CLOSED

d3 avatar d3 commented on April 20, 2024
Indian Number System format

from d3-format.

Comments (18)

curran avatar curran commented on April 20, 2024

This is great! Makes me so happy. Thanks for adding this issue.

from d3-format.

mbostock avatar mbostock commented on April 20, 2024

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.

curran avatar curran commented on April 20, 2024

Here's a first stab an an implementation http://bl.ocks.org/curran/70348c8af4c1959c1289a20ff0a63490

screen shot 2016-04-30 at 9 35 13 pm

from d3-format.

rohithdaka avatar rohithdaka commented on April 20, 2024

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.

Harshit20051 avatar Harshit20051 commented on April 20, 2024

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.

curran avatar curran commented on April 20, 2024

@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:

image

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.

mbostock avatar mbostock commented on April 20, 2024

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.

rohithdaka avatar rohithdaka commented on April 20, 2024

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.

mbostock avatar mbostock commented on April 20, 2024

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.

rohithdaka avatar rohithdaka commented on April 20, 2024

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.

mbostock avatar mbostock commented on April 20, 2024

Sounds good!

from d3-format.

curran avatar curran commented on April 20, 2024

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.

curran avatar curran commented on April 20, 2024

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.

curran avatar curran commented on April 20, 2024

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.

vkbansal avatar vkbansal commented on April 20, 2024

@curran why make this d3 specific. This can be used independent of d3.

from d3-format.

curran avatar curran commented on April 20, 2024

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.

mbostock avatar mbostock commented on April 20, 2024

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.

Liggliluff avatar Liggliluff commented on April 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.