Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

比特币、以太仿 - 加密货币与法定货币转换 #7

Open
threetown opened this issue Sep 9, 2018 · 0 comments
Open

比特币、以太仿 - 加密货币与法定货币转换 #7

threetown opened this issue Sep 9, 2018 · 0 comments

Comments

@threetown
Copy link
Contributor

threetown commented Sep 9, 2018

https://free.currencyconverterapi.com/api/v6/convert?q=USD_CNY

算法函数

  • 法定货币
function format_fiat(val) {
    if (val >= 100000 || val == 0) {
        val = Formatter.toLocaleString(Math.round(val));
    } else if (val >= 1) {
        val = Formatter.toLocaleString(val, {
            minDecimalPlaces: 2,
            maxDecimalPlaces: 2
        });
    } else if (val < 0.000001) {
        val = Number(val).toExponential(2)
    } else {
        val = Formatter.toLocaleString(val, {
            minDecimalPlaces: 6,
            maxDecimalPlaces: 6
        });
    }
    return val;
}
  • 加密货币
function format_crypto(val) {
    return format_crypto_helper(val, 0.00000001, 3, 8, 8)
}

function format_crypto_helper(val, expenonentialThreshold, exponentialDigits, minDecimalPlaces, maxDecimalPlaces) {
    if (val >= 1000 || val == 0) {
        val = Formatter.toLocaleString(Math.round(val));
    } else if (val >= 1) {
        val = Formatter.toLocaleString(val, {
            minDecimalPlaces: minDecimalPlaces,
            maxDecimalPlaces: maxDecimalPlaces
        });
    } else {
        if (val < expenonentialThreshold) {
            val = Number(val).toExponential(exponentialDigits)
        } else {
            val = Formatter.toLocaleString(val, {
                minDecimalPlaces: minDecimalPlaces,
                maxDecimalPlaces: maxDecimalPlaces
            });
        }
    }
    return val;
}


function format_crypto_graph_label(val) {
    return format_crypto_helper(val, 0.00000001, 3, 2, 8)
}

function format_crypto_short(val) {
    return format_crypto_helper(val, 0.01, 1, 2, 2)
}
  • 格式化
var Formatter = (function() {
    var _locale = Cookies.get('_locale') || undefined;
    var supportsLocaleOptions = !!(typeof Intl == 'object' && Intl && typeof Intl.NumberFormat == 'function');
    var _toLocaleString = function(val) {
        if (supportsLocaleOptions) {
            return val.toLocaleString(_locale);
        }
        return val.toLocaleString();
    };
    var _toLocaleStringWithDecimalPlaces = function(val, minDecimalPlaces, maxDecimalPlaces) {
        if (supportsLocaleOptions) {
            return val.toLocaleString(_locale, {
                minimumFractionDigits: minDecimalPlaces,
                maximumFractionDigits: maxDecimalPlaces
            });
        }
        return val.toFixed(maxDecimalPlaces);
    };
    var toLocaleString = function(value, options) {
        var num = Number(value);
        if (isNaN(num)) {
            return value;
        }
        var minDecimalPlaces = options && options.minDecimalPlaces;
        var maxDecimalPlaces = options && options.maxDecimalPlaces;
        if (minDecimalPlaces === undefined || maxDecimalPlaces === undefined) {
            return _toLocaleString(num);
        }
        return _toLocaleStringWithDecimalPlaces(num, minDecimalPlaces, maxDecimalPlaces);
    }
    return {
        toLocaleString: toLocaleString
    };
}
)();

业务代码

  • 调用 coinmarketcap 接口,获取加密货币信息
var tickerData = {};

var apiDomain = "https://api.coinmarketcap.com";
$.ajax({
  url: apiDomain + "/v1/ticker/?limit=0",
}).done(function( data ) {
  //Load Crypto Rates 加密货币
  for (i=0; i < data.length; i++) {
    tickerData[data[i]['id']] = data[i]['price_usd'];
  }
  //Load Fiat Rates 法定汇率(暂不考虑此汇率)
  var fiatRates = $("#currency-exchange-rates").data();
  for (currency in fiatRates) {
    tickerData["__fiat-" + currency] = fiatRates[currency]
  }

  // 业务逻辑,开始转换啦!
  convert();

}).fail(function( data ) {
  console.error(data);
});
  • convert()
function convert() {
  var form = getFormValues();
  var fromId = form.fromId;                   //  "ethereum"
  var fromName = form.fromName;      //  "Ethereum (ETH)"
  var toId = form.toId;                           //  "bitcny"
  var toName = form.toName;              //  "bitCNY (BITCNY)"
  var conversionAmount = form.conversionAmount;                // 1

  if (isNaN(conversionAmount) || conversionAmount == "") {
    conversionAmount = 0;
  }
  conversionAmount = parseFloat(conversionAmount);

  var fromAmount = tickerData[fromId];                                    //  "ethereum"
  var toAmount = tickerData[toId];                                             //  "bitcny"
  var convertedAmount = conversionAmount * fromAmount / toAmount;     // convertedAmount = 1389.3264952911668, conversionAmount = 1

  if (toId.substring(0,6) == "__fiat") {
    convertedAmount = format_fiat(convertedAmount);
  } else {
    convertedAmount = format_crypto(convertedAmount);       convertedAmount = "1,389"
  }

  var resultLeft = conversionAmount + " " + fromName;
  $('#conversion-result-left').html(resultLeft);
  $('#conversion-result-value').html(convertedAmount);
  $('#conversion-result-value-currency').html(toName);
}
@threetown threetown changed the title 比特币、以太仿 比特币、以太仿 - 加密货币与法定货币转换 Sep 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant