https://github.com/mesqueeb/commafy-anything
Return a number as string with , or K. A simple and small integration
https://github.com/mesqueeb/commafy-anything
comma commafy number-format numbers-to-text typescript
Last synced: 11 months ago
JSON representation
Return a number as string with , or K. A simple and small integration
- Host: GitHub
- URL: https://github.com/mesqueeb/commafy-anything
- Owner: mesqueeb
- License: mit
- Created: 2019-06-01T22:32:34.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2025-02-19T18:42:15.000Z (over 1 year ago)
- Last Synced: 2025-06-09T17:42:47.012Z (about 1 year ago)
- Topics: comma, commafy, number-format, numbers-to-text, typescript
- Language: TypeScript
- Homepage: https://npmjs.com/commafy-anything
- Size: 976 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Commafy anything 🍡
```
npm i commafy-anything
```
Return a number as string with `,` or `K`. A simple and small integration.
## Motivation
U built this package because I needed to add comma's and K to numbers! And I wanted to build it myself. 😄
## Meet the family (more tiny utils with TS support)
- [is-what 🙉](https://github.com/mesqueeb/is-what)
- [is-where 🙈](https://github.com/mesqueeb/is-where)
- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)
- [check-anything 👁](https://github.com/mesqueeb/check-anything)
- [remove-anything ✂️](https://github.com/mesqueeb/remove-anything)
- [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything)
- [map-anything 🗺](https://github.com/mesqueeb/map-anything)
- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
- [case-anything 🐫](https://github.com/mesqueeb/case-anything)
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
- [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything)
## Usage
```js
import { commafy } from 'commafy-anything'
commafy(1000) === '1,000'
commafy(10000) === '10,000'
commafy(100000) === '100,000'
commafy(1000000) === '1,000,000'
// etc.
```
### K
You can show numbers as 1K.
```js
const options = { K: true }
// when smaller than 1000 will be shown as is, without K
commafy(123, options) === '123'
// when larger than 1000 it will round up/down behind the K
commafy(1234, options) === '1K'
commafy(10234, options) === '10K'
commafy(100234, options) === '100K'
commafy(1000234, options) === '1,000K'
commafy(1955, options) === '2K'
commafy(10955, options) === '11K'
commafy(100955, options) === '101K'
commafy(1000955, options) === '1,001K'
```
### Thousands
You can disable a comma to be added when the number is between `1000` ~ `9999`.
```js
// default:
commafy(1000) === '1,000'
const options = { thousandsComma: false }
commafy(1000, options) === '1000'
commafy(9999, options) === '9999'
// beyond 9999 it will always have a comma
commafy(10000, options) === '10,000'
```
### Spaced decimals
You can add spaces to decimals.
```js
// default:
commafy(1.0001) === '1.0001'
commafy(1.00001) === '1.00001'
commafy(1.000001) === '1.000001'
commafy(1.0000001) === '1.0000001'
// spaced decimals:
const options = { spacedDecimals: true }
commafy(1.0001, options) === '1.0001'
commafy(1.00001, options) === '1.000 01'
commafy(1.000001, options) === '1.000 001'
commafy(1.0000001, options) === '1.000 0001'
```
### Strip decimals
You can add strip away decimals.
```js
// default:
commafy(1.0001) === '1.0001'
// strip decimals:
const options = { stripDecimals: true }
commafy(1.001, options) === '1'
commafy(1.999, options) === '1'
```
## Source code
I'm using simple regular expressions. The source code is in TypeScript, but the essense of my source code looks something like this:
```js
function commafy(num, { stripDecimals, spacedDecimals } = {}) {
const str = num.toString().split('.')
if (str[0].length >= 4) {
str[0] = str[0].replace(/(\d)(?=(\d{3})+$)/g, '$1,')
}
if (stripDecimals) return str[0]
if (spacedDecimals && str[1] && str[1].length >= 5) {
str[1] = str[1].replace(/(\d{3})/g, '$1 ').trim()
}
return str.join('.')
}
```