簡體   English   中英

為什么toPrecision返回一個String?

[英]Why does toPrecision return a String?

查看此代碼:

function testprecision(){
    var isNotNumber = parseFloat('1.3').toPrecision(6);
    alert(typeof isNotNumber); //=> string
}

我原以為是一個數字。 如果'isNotNumber'應該是一個實數,重鑄是解決方案:

alert(typeof parseFloat(isNotNumber)) //=> number

[編輯]感謝您的回答。 我總結說,精度不是那么精確。 它可以表示數字總位數小數位數 荷蘭的大多數人(我來自哪里)都會想到“分數位數”的精確度。 javascript toPrecision方法涉及第一個表示,因此這是令人困惑的。 無論如何,這種方法可以引入“虛假精度”,對嗎? 對於第二個含義,我們有固定,同樣適用於此(返回字符串,錯誤精度的可能性)。

無論如何,我重新發明輪子是我的主要愛好,我利用我在這里收集的知識,圍繞構建一個javascript浮動對象。 也許它對那里的某個人有用,或者你們中的一個人有更好的想法?

function Float(f,nDec) {
  var Base = this,val;
  setPrecision( nDec || 2 );      
  set( f || 0, nDec || Base.precision );
  Base.set    = set;
  Base.ndec   = setPrecision;
  /** public setprecision 
   *  sets a value for the number of fractional
   *  digits (decimals) you would like getf to 
   *  return. NB: can't be more than 20.
   *  Returns the Float object, so allows method 
   *  chaining
   *  @param {Number} iPrecision
   */
  function setPrecision(iPrecision) {
      var ix = parseInt(iPrecision,10) || 2;
       Base.precision = ix >= 21 ? 20 : ix;
       return Base;
  }
  /** public set
   *  sets the 'internal' value of the object. Returns
   *  the Float object, so allows method chaining
   *  @param {Number} f
   *  @param {Number} ndec
   */
  function set(f,ndec) {
       val =  parseFloat(f) || 0;
       if (ndec) { setPrecision(ndec); }
       Base.val = val;
       return Base;
  }
  /** public get: 
   * return number value (as a float)
   */
  Base.get = function(){
      var ndec = Math.pow(10,Base.precision),
          ival = parseInt(val*ndec,10)/ndec;
      Base.val = ival;
      return Base.val;
  };
  /** public getf 
   *  returns formatted string with precision
   *  (see Base.setPrecision)
   *  if [hx] is supplied, it returns
   *  the float as hexadecimal, otherwise
   *  @param {Boolean} hx
   */
  Base.getf = function(hx){
      var v = Base.val.toFixed(Base.precision);
      return hx ? v.toString(16) : v;
  };
  /** public add
   * adds [f] to the current value (if [f] is a
   * Float, otherwise returns current value)
   * optionally sets a new number of decimals
   * from parameter [ndec]
   * @param {Number} f
   * @param {Number} ndec
   */
  Base.add = function(f,ndec){
      if ( parseFloat(f) || val===0) {
           set(Base.val+parseFloat(f));
           if (ndec) { setPrecision(ndec);}
      }
     return Base.get();
  };
  /** toString 
   *  returns the internal value of the Float object
   *  functions like a getter (supposedly)
   */
  Base.toString = Base.get;
}

使用/例如:

var xf = new Float(); //=> value now 0.0
xf.set(0.86/0.8765,17).add(3.459);
alert(xf+'|'+xf.getf()); //=> 4.440175128351398|4.44017512835139800

從文檔:“將表示Number對象的字符串返回到指定的精度。”

toPrecision()似乎用於格式化輸出,在這種情況下,字符串是最合理的結果。 它代表了一種不會被進一步操縱所破壞的形式的最終輸出。

如果你想要計算精度的一些截斷,我傾向於乘以10 ^ n,其中n是我想要保留的數字,從中取一個整數然后再用相同的方式除。 但這並不完美:在某些情況下,您可能會邀請溢出。 坦率地說,我更喜歡在服務器上進行更復雜的財務計算,我有貨幣,二進制編碼的十進制或類似的數字類型。

假設你有一個像'1.6'的數字。 如果將其格式化為右側有6個零,則會得到“1.600000”。 對於計算機,它仍然是與1.6相同的數字,但是對於您和您的網站,如果您的所有數字都具有不同的長度(例如,這可能會傷害解析器),則不一樣。

因此,為了避免它,toPrecision返回一個字符串,否則解釋器會將數字重新格式化為“1.6”。

toPrecision的目的是將Number的有效十進制數字截斷為指定的數量。 但是Number s的內部表示的數據類型是二進制 IEEE-754 double。 因此,大多數時候不可能精確的返回值存儲在Number中。 由於這種不精確性,返回值將具有無限量的十進制數字,這將導致toPrecision無效。

因此,解決此問題的唯一合理方法是返回十進制數字。 目前唯一合理的十進制數字JS數據類型是String。


這是一個例子,用於澄清Number s的不精確性,如果用於十進制數字:

// the following looks like something with 2 decimal digits:
var number = 1.6;

// but in fact it's a number with an infinite amount of decimal digits.
// let's look at the first 30 of them:
alert(number.toPrecision(30));

// 1.60000000000000008881784197001

因為它是格式化功能。

你需要一個字符串來尾隨零。 貨幣展示就是一個很好的例子。

問題是使用toPrecision 不用試試吧。

var isNotNumber = parseFloat('1.3');
alert(typeof isNotNumber); //=> number

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM