Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
How to display localized (best fit) units with Intl?

When working with dates it automatically localizes them, i.e. my local date would be YYYY-MM-DD while with en-US locale I would receive DD/MM/YY.

When working with length, area, weight, volume, temperature, time and their compound units like length-per-time I can't find a similar feature in Intl API. I've been searching specs and github for whole day, but the closest thing I found was https://github.com/tc39/ecma402/issues/32 :

var f = Intl.UnitFormat(navigator.languages, {
  units: {
    {type: 'length', unit: 'bestFit'},
  }
});
f.format(2048); // would return '2 km' in en-US

Which seems to be gone after merging into "Intl.NumberFormat rev. 2".

So is there a way to avoid mapping every existing locale with every existing unit like in this example?:

const format = unit => value =>
  new Intl.NumberFormat("en", {
    style: "unit",
    unit,
    notation: "compact",
    maximumSignificantDigits: 3
  })
  .format(value)


function fakeBestFit(value, locale) {
  // if US or any other imeprial system based locale
  if (locale || navigator.language === 'en-US') {
    const mile = 1609.34
    const yard = 0.9143977272588
    // const inch = ...
    // const foot = ...
    if (value >= mile) {
      return format("mile")(value / mile)
    } else if (value >= yard) {
      return format("yard")(value / yard)
    } else {
      // other units for locale i.e. inches and feet
    }
  } else {
    // for metric countries
    return format("meter")(value)
  }
}

console.log('expected: 1 m, received: ' + fakeBestFit(1))
console.log('expected: 1 km, received: ' + fakeBestFit(1000))
console.log('expected: 1 Mm, received: ' + fakeBestFit(1000000))
console.log('expected: 1 cm, received: ' + fakeBestFit(0.001))
console.log('expected: 1 mi, received: ' + fakeBestFit(1609.34, 'en-US'))
console.log('expected: 1 yd, received: ' + fakeBestFit(0.9143977272588, 'en-US'))

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!