曾经参加高德一道面试题,怎么把类似(175.46,37.876)坐标转化为简单的字符串来表示,当时还认为这是一道非常牛逼的题目,非常考察一个人的思维能力。回来的路上,想了一下,不就是考察了String的两个方法吗!fromCharCode和charCodeAt。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
<script type= "text/javascript" > function
coordinateToStr(x,y){ x=x.toFixed(4); y=y.toFixed(4); x=x.toString().split( "." ); y=y.toString().split( "." ); return
String.fromCharCode(x[0])+String.fromCharCode(x[1])+String.fromCharCode(y[0])+String.fromCharCode(y[1]); } function
strToCoordinate(str){ var
t=[]; t.push(str.charCodeAt(0)); t.push(str.charCodeAt(1)); t.push(str.charCodeAt(2)); t.push(str.charCodeAt(3)); return
{ x:Number(t[0]+ "." +t[1]), y:Number(t[2]+ "." +t[3]) } } var
str=coordinateToStr(175.46,37.876); console.log(str); var
coo=strToCoordinate(str); console.log(coo); </script> |
原文:http://www.cnblogs.com/HPhone/p/3528428.html