Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert data with k and M at the end back to numbers in json array

I am getting data from an API in jso format but one of the element is giving values in k and M instead of thousand and million.
how can I convert it back to number.

k = [
{
'stock_change_volume' : '169.2 K
'
},
{
'stock_change_volume' : '69.2 K
'
},
{
'stock_change_volume' : '169.2 M
'
}

]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var k = [
{
'stock_change_volume' : '169.2 K'
},
{
'stock_change_volume' : '69.2 K'
},
{
'stock_change_volume' : '169.2 M'
}
];

for(var i of k){
  console.log(getVal(i['stock_change_volume']));
}

function getVal (val) {
  var value = val.substr(-1).toLowerCase();
  if (value == "k")
    return parseFloat(val) * 1000;
  else if (value == "m")
    return parseFloat(val) * 1000000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading