Menu

Compact forms

Numeric
2016-02-01
2016-02-08
  • Philip Olsén

    Philip Olsén - 2016-02-01

    Hi,

    Is there any way to format numbers like "1000000" to be "1M" using iLib?

     
  • Edwin H

    Edwin H - 2016-02-02

    That is similar to what the UnitFmt class does with the autoScale flag turned on, but I think you are talking about plain numbers with no units, right? If so, then ilib does not do that yet.

    What is it you're counting? Ilib currently handles autoscaling for these types of measures:

    • Area
    • Digital Storage (as in bytes, megabytes, etc.)
    • Energy
    • Fuel Consumption
    • Length
    • Mass
    • Temperature
    • Time
    • Velocity
    • Volume

    Others can be added easily if you let us know what you are counting.

     
    • Philip Olsén

      Philip Olsén - 2016-02-03

      Yeah I have had a look at the UnitFmt class but not quite got the result i wanted. As you said this is just plain numbers that I would like to have in compact form, like;

      en-US 1000 =>"1K", 10.000 => "10K", 100.000 => "100K"

      or could also be with currency

      $1000000 -> $1M

      But I have not tried with the autoScale flag on, maybe I should give that a shot.

       
  • Edwin H

    Edwin H - 2016-02-04

    I think there should probably be an "autoScale" option for the number formatter (NumFmt) to handle this. That would get plain unit-less numbers, as well as currency and percentages as well.

    I'm looking in CLDR 28, and I see the right data for that sort of thing in the numbers.json for many locales. Here's the one for English as an example:

    "decimalFormats-numberSystem-latn": {
      "standard": "#,##0.###",
      "long": {
        "decimalFormat": {
          "1000-count-one": "0 thousand",
          "1000-count-other": "0 thousand",
          "10000-count-one": "00 thousand",
          "10000-count-other": "00 thousand",
          "100000-count-one": "000 thousand",
          "100000-count-other": "000 thousand",
          "1000000-count-one": "0 million",
          "1000000-count-other": "0 million",
          "10000000-count-one": "00 million",
          "10000000-count-other": "00 million",
          "100000000-count-one": "000 million",
          "100000000-count-other": "000 million",
          "1000000000-count-one": "0 billion",
          "1000000000-count-other": "0 billion",
          "10000000000-count-one": "00 billion",
          "10000000000-count-other": "00 billion",
          "100000000000-count-one": "000 billion",
          "100000000000-count-other": "000 billion",
          "1000000000000-count-one": "0 trillion",
          "1000000000000-count-other": "0 trillion",
          "10000000000000-count-one": "00 trillion",
          "10000000000000-count-other": "00 trillion",
          "100000000000000-count-one": "000 trillion",
          "100000000000000-count-other": "000 trillion"
        }
      },
      "short": {
        "decimalFormat": {
          "1000-count-one": "0K",
          "1000-count-other": "0K",
          "10000-count-one": "00K",
          "10000-count-other": "00K",
          "100000-count-one": "000K",
          "100000-count-other": "000K",
          "1000000-count-one": "0M",
          "1000000-count-other": "0M",
          "10000000-count-one": "00M",
          "10000000-count-other": "00M",
          "100000000-count-one": "000M",
          "100000000-count-other": "000M",
          "1000000000-count-one": "0B",
          "1000000000-count-other": "0B",
          "10000000000-count-one": "00B",
          "10000000000-count-other": "00B",
          "100000000000-count-one": "000B",
          "100000000000-count-other": "000B",
          "1000000000000-count-one": "0T",
          "1000000000000-count-other": "0T",
          "10000000000000-count-one": "00T",
          "10000000000000-count-other": "00T",
          "100000000000000-count-one": "000T",
          "100000000000000-count-other": "000T"
        }
      }
    },
    

    A quick scan of other common locales shows that the data is reasonably translated for all of them. So, this should be quite do-able.

    I'll add it to the backlog. I won't be able to get to it soon, though. (Anyone want to contribute?)

     

    Last edit: Edwin H 2016-02-04
  • Edwin H

    Edwin H - 2016-02-04

    Oh, and the autoscale option for the UnitFmt class is on by default, so if you've already looked at UnitFmt, then it probably won't work for your needs as-is.

     
    • Philip Olsén

      Philip Olsén - 2016-02-08

      Okey, thanks for the information. It would be nice to have that type of functionality added to the NumFmt. But I understand that you have alot on your plate :]

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.