2011-11-11 35 views
0

我遇到了一個奇怪的不想要的NumberNaN轉換,不知道在哪裏和爲什麼。我顯然在這裏錯過了一些東西,但我無法弄清楚什麼。jquery - 奇怪的數字到字符串轉換

任何建議是非常歡迎! ;)

下面是javascript代碼:

updatedAbsoluteResult = currentlyDisplayedRentability * investment 
resultAgainstReferencial = updatedAbsoluteResult - appropriateReferencialRentability * investment 

$('#a0').html('currentlyDisplayedRentability: ' + typeof currentlyDisplayedRentability) 
$('#a1').html('investment: ' + typeof investment) 
$('#a2').html('updatedAbsoluteResult: ' + typeof updatedAbsoluteResult) 
$('#a3').html('appropriateReferencialRentability: ' + typeof appropriateReferencialRentability) 
$('#a4').html('resultAgainstReferencial: ' + typeof resultAgainstReferencial) 
$('#a5').html('resultAgainstReferencial: ' + resultAgainstReferencial) 

下面是HTML輸出:

currentlyDisplayedRentability: number 
investment: number 
updatedAbsoluteResult: number 
appropriateReferencialRentability: number 
resultAgainstReferencial: number 
resultAgainstReferencial: NaN 

一切都是數字類型的,但是當我想返回最終結果,我得到'不是一個數字'。 任何人都知道這是爲什麼?

享受你的週末!

+0

它可能相關,也可能不相關,但是您沒有向我們展示「適當的參考可重定義」和「投資」的定義? – Widor

+0

這個數字的一​​些例子會有幫助... –

+1

你應該在每一行的末尾使用終結符(';')。 –

回答

2

數字類型可以返回NaN,並且仍然是數字類型。檢查updatedAbsoluteResult,appropriateReferencialRentability和投資的價值,看看是否有這些都是意外的值。有可能這個等式中的某些東西出了問題。

0

NaN在JavaScript技術上仍然是number類型。它通常是從除以0這樣做造成的另一個NaN任何工科數學計算也將返回NaN

0

也許具有諷刺意味的是,NaN本身就是Number

您可能在某處遇到了一個失敗的parseInt()調用,或許多其他可能性。沒有看到相關代碼很難猜測。

1

問題很可能是resultAgainstReferencial是NaN。您所得到的結果,你是因爲:

typeof NaN == "number" 

要告訴你:

resultAgainstReferencial = NaN; 
alert(typeof resultAgainstReferencial); // alerts "number" 

你甚至可以看到在這裏:http://jsfiddle.net/jfriend00/t3ubg/

因此,上游某處,你正在試圖做的數學與非數字的東西。你必須看的所有的數字輸入值,看看數據已經歪。