2012-11-08 48 views
1

我在if條件中使用變量時遇到問題。我有三個變量,其中一個是字符串類型,另外兩個是Json。這裏settings.DecimalDigits值是2或任何大於2將字符串轉換爲浮動數字Javascript

var controlValue = integer + '.' + mantissa; 
controlValue = parseFloat(controlValue).toFixed(settings.DecimalDigits); 

整數&尾數將具有存儲在controlValue作爲字符串一定值。 controlValue然後與其他兩個變量(settings.MaxValue & settings.MinValue)在IF條件,但通過其狀況不會比它的類型是字符串類型

if (controlValue > settings.MaxValue) 
       controlValue = settings.MaxValue; 

      if (controlValue < settings.MinValue) 
       controlValue = settings.MinValue; 

在我分析這三個變量將在浮動型

controlValue = 123.23 123.00或
settings.MaxValue = 99.99
settings.MinValue = -99.99 0123三個值 請幫忙,這樣解析穿過IF條件

回答

1

jfriend00's answer幫我解決了我的問題。下面的解決方案:

  var controlValue = e.target.value; //get value from input 
      controlValue = Number(controlValue); //Converting the string to number 

      // Number format parses through if condition 
      if (controlValue > settings.MaxValue) 
       controlValue = Number(settings.MaxValue); 
      if (controlValue < settings.MinValue) 
       controlValue = Number(settings.MinValue); 

      // if the value is having a mantissa 00. It will be rejected by Number() function. So next line its converted again to string format using .toFixed() function. 

      var controlValue = controlValue.toFixed(settings.DecimalDigits); 

      // Splitting the value into two parts integer and mantissa 
      integer = controlValue.split('.')[0]; 
      if (typeof integer == 'undefined' || integer == null || integer == "" || isNaN(integer)) 
       integer = 0; 

      // settings.DecimalDigits is the variable to set any default number of mantissa required to appear. 
      mantissa = controlValue.split('.')[1]; 
      if (typeof mantissa == 'undefined') { 
       mantissa = ""; 
       for (i = 0; i < settings.DecimalDigits; i++) 
        mantissa += '0'; 
      } 

      // Finally you have the result 
      controlValue = integer + '.' + mantissa; 
+2

嗯,所以我幫你解決這個問題,沒有其他人提供任何幫助。你從我的答案中獲得一些信息,並將其納入你的解決方案(一件好事),然後你最終從我的答案中拿走任何信用,所以我根本沒有收到任何聲譽信用,因爲你在這裏相對較新,也許你不明白SO是如何工作的,但是如果你獎勵那些幫助你的人,它通常會更好。 – jfriend00

+0

Hi @ jfriend00,我試着點擊你的答案的投票,它給出了一個表示你需要15點聲望的方法。 :)無論如何感謝很多人......但答案沒有多大幫助。然而,找到了解決辦法。:) – teenu

+0

是的,當您的聲譽如此之低時,您可以獎勵回答者的唯一方法就是接受他們的答案。回答其他人的問題可以獲得更多的聲譽。 – jfriend00

2

.toFixed()將您的號碼回字符串。如果你想再次回到一個數字,那麼你需要使用parseFloat。可能有更好的方法來做到這一點,但建立在現有的代碼,你會作出controlValue一個數字,將在您的if聲明中再次合作,通過調用parseFloat()這樣的:

var controlValue = integer + '.' + mantissa; 
controlValue = parseFloat(parseFloat(controlValue).toFixed(settings.DecimalDigits)); 

僅供參考,它可能會更有意義剛剛處理的數量完全爲數字,而不是來回走爲字符串幾次:

var controlValue = parseFloat(integer + '.' + mantissa); 
var filter = Math.pow(10, settings.DecimalDigits); 
controlValue = Math.round(controlValue * filter)/filter; 

或者甚至只是這樣的:

var controlValue = parseFloat(integer + '.' + mantissa.toString().substr(0, settings.DecimalDigits)); 
+0

酷.... :)我打破了我的頭這個簡單的解決方案。 lols ...謝謝@ jfriend00 – teenu

+0

@ShishirKumarM - 我給我的答案增加了幾個選項。 – jfriend00

+0

我在尋找一個返回結果爲213.00的結果,其中數字應該跟隨**「。00」**。你能幫我解決這個問題嗎?如果「」之後的結果不是00,結果將返回尾數。 – teenu