Skip to content

Units of Measures

Phil Schatzmann edited this page Feb 21, 2022 · 8 revisions

I added some simple logic to handle units of measures in the NumberUnitToText class. The logic is based on 4 different array which contain the basic information:

  • unit_1 is used to handle units that can have a singular and plural: e.g. one dollar but it is two dollars

    • "lb", "pound", "pounds"
    • "ft", "foot", "feet"
    • "in", "inch", "inches"
    • "mi", "mile", "miles"
    • "$", "dollar", "dollars"
    • "¢", "cent", "cents"
  • unit_2: is used to assemble units from (potentially) multiple words

    • "mph", "miles", "per", "hour"
    • "C", "degrees", "celcius"
    • "F", "degrees", "fahrenheit"
    • "%", "percent"
    • "lt", "liter"
    • "ml", "milli", "lt"
    • "gr", "gram"
    • "kg", "kilo", "gr"
    • "mg", "milli", "gr"
    • "km", "kilo", "m"
    • "m", "meter"
    • "cm", "centi", "m"
    • "mm", "milli", "m"
    • "usd", "u.s.", "$"
  • unit3: split unit before and after the decimal point: 1.20 USD is 1 Dollar and 20 Cents

    • "usd", "¢"
    • "gr", "mg"
    • "kg", "gr"
    • "m", "mm"
    • "km", "m"
    • "lt", "ml"
  • decimal_info: Units might have different decimals. The decimal system is based on 3. But currencies have usually 2.

    • "usd", 2
    • (default), 3

This is used e.g.

  • for USD 0.2: This gives 0.20 which gives 20 cents.
  • for kg 0.2: This gives 0.200 which gives 200 grams.

I did not want to provide a complete set of all possible units, but provides at least the ones which might be of daily use. Please feel free to submit a pull request if you think some critical unit is missing!

Clone this wiki locally