question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compact Decimal Format

See original GitHub issue

👋 I worked on a solution a few months ago to add short number formatting to a library similar to this and have this working in some applications.

https://github.com/snewcomer/cldr-compact-number

I’m not familiar with the internals of this library so I wanted to float the idea by you if you thought it might be a good addition.

Convert numbers like -

  • 101234 to 101,1 mil in Espanol
  • 101234 to 10.1万 in Japanese

https://github.com/ember-intl/intl-messageformat/pull/1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
snewcomercommented, Apr 23, 2019

@eemeli Perfect! Yeah I’ve been tracking the work that has gone in! 👍 Hopefully within the next month I’ll be able to work on this.

0reactions
eemelicommented, Apr 5, 2021

This issue has languished here for ages, but with #313 it should finally become easy to provide a solution for this. As a first-pass solution, something like this seems to work:

// compact.js
import { compactFormat } from 'cldr-compact-number';

const localeData = {};

export const compact = (value, locale, options = {}) =>
  compactFormat(value, locale, localeData, options);
// rollup.config.js
import alias from '@rollup/plugin-alias';
import messageformat from 'rollup-plugin-messageformat';
import { compact } from './compact';

const customFormatters = {
  compact: { formatter: compact, arg: 'options', id: 'compact', module: '~compact' }
};

export default {
  input: 'src/main.js',
  output: { dir: 'dist' },
  plugins: [
    alias({ entries: { '~compact': './compact.js' } }),
    messageformat({ customFormatters, locales: ['en'] })
  ]
};
# src/messages.properties
compact = Only {n, compact, significantDigits: 1}
// src/main.js
import messages from './messages.properties';

console.log(messages.compact({ n: 424567546 }));

When compiled, that produces this bundle:

// dist/main.js
import { compactFormat } from 'cldr-compact-number';

const localeData = {};

const compact = (value, locale, options = {}) =>
  compactFormat(value, locale, localeData, options);

var messages = {
  compact: (d) => "Only " + compact(d.n, "en", {"significantDigits":1})
};

console.log(messages.compact({ n: 424567546 }));

And that, when executed, prints out this:

Only 424.6M
Read more comments on GitHub >

github_iconTop Results From Across the Web

CompactDecimalFormat | Android Developers
The CompactDecimalFormat produces abbreviated numbers, suitable for display in environments will limited real estate. For example, 'Hits: 1.2B' instead of ...
Read more >
Compact Number Formatting in Java
Compact number formatting refers to the representation of a number in a shorter form based on the patterns provided for a given locale....
Read more >
CompactDecimalFormat (ICU4J 72.1)
The CompactDecimalFormat produces abbreviated numbers, suitable for display in environments will limited real estate. For example, 'Hits: 1.2B' instead of ...
Read more >
Can CompactDecimalFormat somehow be used to format ...
I'm trying to use ICU4J's CompactDecimalFormat to format values like "12345.67" into "$12K". I can't seem to see how to include the currency...
Read more >
[Proposal] Compact Decimal Format to abbreviate large ...
[Proposal] Compact Decimal Format to abbreviate large numbers #37. Closed. caridy opened this issue on Sep 18, 2015 · 12 comments.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found