|
3 | 3 | var ipcRenderer = require('electron').ipcRenderer, |
4 | 4 | $ = require('jquery'), |
5 | 5 | Socket = require('./utils/Socket'), |
| 6 | + btcConvert = require('bitcoin-convert'), |
6 | 7 | _app; |
7 | 8 |
|
8 | 9 |
|
@@ -125,11 +126,43 @@ App.prototype.setUnreadChatMessageCount = function(count) { |
125 | 126 | } |
126 | 127 | }; |
127 | 128 |
|
128 | | -App.prototype.intlNumFormat = function(numberToFormat, maxDigits){ |
129 | | - maxDigits = maxDigits || 8; //default to show down to the satoshi (.00000001) |
| 129 | +App.prototype.intlNumFormat = function(numberToFormat, maxDigits = 8) { |
130 | 130 | return new Intl.NumberFormat(window.lang, {maximumFractionDigits: maxDigits}).format(numberToFormat); |
131 | 131 | }; |
132 | 132 |
|
| 133 | +App.prototype.getBitcoinUnit = function() { |
| 134 | + if (!this._bitcoinUnit) { |
| 135 | + this._bitcoinUnit = localStorage.getItem('BitcoinUnit') || 'BTC'; |
| 136 | + } |
| 137 | + return this._bitcoinUnit; |
| 138 | +}; |
| 139 | + |
| 140 | +App.prototype.setBitcoinUnit = function(unit) { |
| 141 | + this._bitcoinUnit = unit; |
| 142 | + localStorage.setItem('BitcoinUnit', unit); |
| 143 | +}; |
| 144 | + |
| 145 | +/** |
| 146 | + * Format a bitcoin amount in the user's locale. |
| 147 | + * |
| 148 | + * @param {Number} amount - Bitcoin (BTC) amount. |
| 149 | + * @param {Number} [maxDigits=8] - Maximum number of fraction digits to display. |
| 150 | + * @returns {String} localised amount ending with ' BTC' or the default bitcoin unit. |
| 151 | + * |
| 152 | + * @see intlNumFormat |
| 153 | + */ |
| 154 | +App.prototype.formatBitcoin = function(amount, maxDigits) { |
| 155 | + let unit = this.getBitcoinUnit(); |
| 156 | + if (unit === 'mBTC') { |
| 157 | + maxDigits = 2; |
| 158 | + } else if (unit !== 'BTC') { |
| 159 | + maxDigits = 0; |
| 160 | + } |
| 161 | + amount = btcConvert(amount, 'BTC', unit); |
| 162 | + return this.intlNumFormat(amount, maxDigits) |
| 163 | + + ' ' + unit; |
| 164 | +}; |
| 165 | + |
133 | 166 | App.getApp = function() { |
134 | 167 | if (!_app) { |
135 | 168 | throw new Error('The app instance was never instantiated and is therefore not available.'); |
|
0 commit comments