我有一个角度代码,可以从谷歌财务中获取数据。但是,如果存在代表数百万和数千的M或K,它可以正确获取vo和avvo的数据。如果没有它,则无法正确显示该值。
如何正确处理这两种情况?
数据示例:
// [
{
"id": "549805688244420"
,"t" : "AZG"
,"e" : "SGX"
,"l" : "0.060"
,"l_fix" : "0.060"
,"l_cur" : "SGD0.06"
,"s": "0"
,"ltt":"5:04PM GMT+8"
,"lt" : "Jun 19, 5:04PM GMT+8"
,"lt_dts" : "2017-06-19T17:04:01Z"
,"c" : "0.000"
,"c_fix" : "0.000"
,"cp" : "0.00"
,"cp_fix" : "0.000"
,"ccol" : "chb"
,"pcls_fix" : "0.06"
,"eo" : ""
,"delay": ""
,"op" : ""
,"hi" : ""
,"lo" : ""
,"vo" : "0.00"
,"avvo" : "2,087.00"
,"hi52" : "0.21"
,"lo52" : "0.05"
,"mc" : "5.75M"
,"pe" : ""
,"fwpe" : ""
,"beta" : ""
,"eps" : "-0.22"
,"shares" : "92.63M"
,"inst_own" : ""
,"name" : "8Telecom International Holdings Co. Ltd."
,"type" : "Company"
}
]
它将显示为N / A和avvo为2.0。 如果数据如下:
// [
{
"id": "381558643124633"
,"t" : "U11"
,"e" : "SGX"
,"l" : "23.25"
,"l_fix" : "23.25"
,"l_cur" : "SGD23.25"
,"s": "0"
,"ltt":"11:24AM GMT+8"
,"lt" : "May 19, 11:24AM GMT+8"
,"lt_dts" : "2017-05-19T11:24:19Z"
,"c" : "-0.09"
,"c_fix" : "-0.09"
,"cp" : "-0.39"
,"cp_fix" : "-0.39"
,"ccol" : "chr"
,"pcls_fix" : "23.34"
,"eo" : ""
,"delay": ""
,"op" : "23.24"
,"hi" : "23.40"
,"lo" : "23.22"
,"vo" : "1.34M"
,"avvo" : "2.47M"
,"hi52" : "24.02"
,"lo52" : "17.41"
,"mc" : "38.29B"
,"pe" : "12.49"
,"fwpe" : ""
,"beta" : ""
,"eps" : "1.86"
,"shares" : "1.64B"
,"inst_own" : ""
,"name" : "United Overseas Bank Ltd"
,"type" : "Company"
}
]
vo和avvo数据正确无误。
angular.module('myapp.factories').factory('$stockDataFactory',
['$q', '$filter', '$webServicesFactory',
function ($q, $filter, $webServicesFactory) {
return {
getStockDetails: function (stock) {
var deferred = $q.defer();
$webServicesFactory.getNotParsed(
"https://www.google.com/finance/info",
{},
{
infotype: "infoquoteall",
q: stock.market+":"+stock.symbol
}
).then(
function success(response) {
response = JSON.parse("["+response.replace(/\r?\n|\r/g, ' ').split('[').splice(1,1)[0]);
stock = response[0];
var formatedData = {
volume: stock.vo,
averageDailyVolume: stock.avvo
};
HTML
<div class="row row-center">
<div class="col boldText">Volume: </div>
<div class="col col-21">{{stock.volume || 'N/A'}}</div>
<div class="col boldText">Avg. Vol: </div>
<div class="col col-21">{{(stock.averageDailyVolume | shrinkNumber: 2) || 'N/A'}}</div>
</div>
javascript中的缩水
angular.module('chaishen.factories').filter("shrinkNumber", function($filter) {
return function(number, fractionSize) {
if(number === null) return null;
if(number === 0) return "0.000";
// default number of decimals for values >1000 or <-1000 if not set in view
if(!fractionSize || fractionSize < 0)
fractionSize = 1;
var abs = Math.abs(number);
var rounder = Math.pow(10,fractionSize);
var isNegative = number < 0;
var key = '';
var powers = [
{key: "Q", value: Math.pow(10,15)},
{key: "T", value: Math.pow(10,12)},
{key: "B", value: Math.pow(10,9)},
{key: "M", value: Math.pow(10,6)},
{key: "K", value: 1000}
];
for(var i = 0; i < powers.length; i++) {
var reduced = abs / powers[i].value;
//reduced = Math.round(reduced * rounder) / rounder;
reduced = Math.round(abs * rounder) / rounder;
if(reduced >= 1){
abs = reduced;
key = powers[i].key;
break;
}
}
if(number >= 1000) {
//return abs + key;
return abs;
}
else if(number <= -1000) {
//return '-' + abs + key;
return '-' + abs;
}
else {
//return $filter('number')(((isNegative ? '-' : '') + abs + key), 3);
return $filter('number')(((isNegative ? '-' : '') + abs), 3);
}
};
});