2011-11-08 3 views
0

단순한 것처럼 보이지만이 질문을 찾을 수 없습니다.양수 또는 음수가있는 백분율 함수

내가 시작 및 끝 위치, 즉 알고 전체 거리의 비율을 계산할 말할 수 있습니다 :

function getPercentage(startpos,endpos,currentpos) 
{ 
    var distance = endpos-startpos; 
    return (currentpos/distance)*100; 
} 

쉽게하지만, 매우 늦게 커피가 startpos 경우 또는 무슨 작동하지 않습니다이다 부정적인 축이 어떤 위치 중 어느 시점에서 양 또는 음의 축이 될 수 있다고 지적

startpos = -734; 
endpos = 65; 
currentpos = -38; 

가치가 IE에서 현재 POS이다. 그것은 당신이뿐만 아니라 현재 위치를 번역하는 것을 잊었다

답변

6

:-) 오전 1시이야 pleasee에서 .... currentpos있는 거리의 몇 퍼센트

는 사람이 나에게 뼈를 던져 수 있습니다.

function getPercentage(startpos, endpos, currentpos) 
{ 
    var distance = endpos - startpos; 
    var displacement = currentpos - startpos; 
    return (displacement/distance) * 100; 
} 
+0

당신을 감사합니다! 나는 잠을 잘 수있다 :-) – Alex

1

쉬운 일이 할 수는 startpos이 제로가되도록 위치를 상쇄하는 것입니다

endpos = endpos - startpos; 
currentpos = currentpos - startpos; 
var perc = currentpos/endpos*100; 
+0

당신에게 감사합니다 !!! 자러 가다 – Alex

관련 문제