Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/triplespeeder/bn2string
Provide ready-to-use display strings for BN.js instances.
https://github.com/triplespeeder/bn2string
Last synced: 23 days ago
JSON representation
Provide ready-to-use display strings for BN.js instances.
- Host: GitHub
- URL: https://github.com/triplespeeder/bn2string
- Owner: TripleSpeeder
- Created: 2019-10-16T08:52:45.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T18:52:34.000Z (almost 2 years ago)
- Last Synced: 2024-04-24T23:20:46.625Z (7 months ago)
- Language: JavaScript
- Size: 28.3 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# bn2string
Provide ready-to-use display strings for BN.js instances.## Installation
```bash
npm install @triplespeeder/bn2string
```## Usage
Assume we have a balance of around 12500 Tether (USDT) tokens. USDT is specified with 6 decimals.
For display in a GUI we want to have the human-readable USDT value, both rounded to 2 decimals and in full precision.```javascript
var BN = require('bn.js')
var bn2DisplayString = require('@triplespeeder/bn2string')const value = new BN('12525652700')
const decimals = new BN('6')
const roundToDecimals = new BN('2')var {precise, rounded} = bn2DisplayString({value, decimals, roundToDecimals})
console.log("Precise balance: " + precise + " USDT")
console.log("Rounded balance: " + rounded + " USDT")
```Output:
```bash
[michael]$ node example.js
Precise balance: 12 525.652700 USDT
Rounded balance: 12 525.65 USDT
```### Changing the decimal separator
By default '.' is used as decimal separator. This can be changed by providing a different symbol, e.g.
```javascript
var {precise, rounded} = bn2DisplayString({value, decimals, roundToDecimals, decimalSeparator:','})
```
Output:
```bash
[michael]$ node example.js
Precise balance: 12 525,652700 USDT
Rounded balance: 12 525,65 USDT
```### Changing digit grouping
By default the integer digits are grouped as triplets and separated by unicode symbol ```u202F```, the "narrow no-break space" (See https://en.wikipedia.org/wiki/Decimal_separator#Digit_grouping for reasoning). This can be changed with the optional parameter 'groupSeparator':
```javascript
var {precise, rounded} = bn2DisplayString({value, decimals, roundToDecimals, groupSeparator: ','})
```
Output:
```bash
[michael]$ node example.js
Precise balance: 12,525.652700 USDT
Rounded balance: 12,525.65 USDT```