简体   繁体   中英

string manipulation in JavaScript

(27.19, 78.01)

i have this information in javascript and need in this form.

27.19N , 78.00E

if 27.19 is positive then include N after them otherwise S then it can be 27.19S

if 78.01 is positive then include E after them otherwise W then it can be 78.01W

how i can do this in javascript.

var data = [27.19, -78.01];
var formatted = data[0] + ((data[0] > 0) ? 'N' : 'S') + ' , ' + 
                data[1] + ((data[1] > 0) ? 'E' : 'W');
alert(formatted);
function convert(tuple) {
   var pairs = tuple.substring(1, tuple.length-1).split(/,\s?/);
   pairs[0] = parseFloat(pairs[0]);
   pairs[1] = parseFloat(pairs[1]);
   return [Math.abs(pairs[0]) + (pairs[0] >= 0 ? "N" : "S"),
           Math.abs(pairs[1]) + (pairs[1] >= 0 ? "E" : "W")].join(" , ");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM