This repository has been archived by the owner on Aug 9, 2019. It is now read-only.
forked from bitpay/insight
/
currency.js
64 lines (51 loc) · 2 KB
/
currency.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
angular.module('insight.currency').controller('CurrencyController',
function($scope, $rootScope, Currency) {
$rootScope.currency.symbol = defaultCurrency;
var _roundFloat = function(x, n) {
if(!parseInt(n, 10) || !parseFloat(x)) n = 0;
return Math.round(x * Math.pow(10, n)) / Math.pow(10, n);
};
$rootScope.currency.getConvertion = function(value) {
value = value * 1; // Convert to number
if (!isNaN(value) && typeof value !== 'undefined' && value !== null) {
if (value === 0.00000000) return '0 ' + this.symbol; // fix value to show
var response;
if (this.symbol === 'USD') {
response = _roundFloat((value * this.factor), 2);
} else if (this.symbol === 'mBTC') {
this.factor = 1000;
response = _roundFloat((value * this.factor), 5);
} else if (this.symbol === 'bits') {
this.factor = 1000000;
response = _roundFloat((value * this.factor), 2);
} else { // assumes symbol is BTC
this.factor = 1;
response = _roundFloat((value * this.factor), 8);
}
// prevent sci notation
if (response < 1e-7) response=response.toFixed(8);
return response + ' ' + this.symbol;
}
return 'value error';
};
$scope.setCurrency = function(currency) {
$rootScope.currency.symbol = currency;
localStorage.setItem('insight-currency', currency);
if (currency === 'USD') {
Currency.get({}, function(res) {
$rootScope.currency.factor = $rootScope.currency.bitstamp = res.data.bitstamp;
});
} else if (currency === 'mBTC') {
$rootScope.currency.factor = 1000;
} else if (currency === 'bits') {
$rootScope.currency.factor = 1000000;
} else {
$rootScope.currency.factor = 1;
}
};
// Get initial value
Currency.get({}, function(res) {
$rootScope.currency.factor = $rootScope.currency.bitstamp = res.data.bitstamp;
});
});