flosight-ui
Version:
An open-source frontend for the Flosight API. The Flosight API provides you with a convenient, powerful and simple way to query and broadcast data on the florincoin network and build your own services with it.
78 lines (60 loc) • 2.4 kB
JavaScript
;
angular.module('flosight.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') {
var USDValue = value * this.factor;
response = USDValue.toFixed(2)
if (parseFloat(response) === 0)
response = USDValue.toFixed(4)
if (parseFloat(response) === 0)
response = USDValue.toFixed(6)
if (parseFloat(response) === 0)
response = USDValue.toFixed(8)
if (parseFloat(response) === 0)
response = 0;
} else if (this.symbol === 'mFLO') {
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 FLO
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('flosight-currency', currency);
if (currency === 'USD') {
Currency.get({}, function(res) {
$rootScope.currency.factor = $rootScope.currency.bitstamp = res.data.bitstamp;
});
} else if (currency === 'mFLO') {
$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;
});
});