当需要0.000000123时,Javascript parseFloat'1.23e-7'给出1.23e-7 [英] Javascript parseFloat '1.23e-7' gives 1.23e-7 when need 0.000000123

查看:164
本文介绍了当需要0.000000123时,Javascript parseFloat'1.23e-7'给出1.23e-7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

parseFloat(1.51e-6);
// returns 0.00000151

parseFloat(1.23e-7);
// returns 1.23e-7
// required 0.000000123

我是排序表列包含各种浮点数,其中一些用科学记数法表示。

I am sorting table columns containing a wide range of floating-point numbers, some represented in scientific notation.

我使用的是jQuery tablesorter2.0插件,它使用'parseFloat'代替以数字开头的单元格。
问题是parseFloat返回表示为1.23e-7的非常小的数字作为字符串,并且不将其扩展为0.000000123。
因此,tablesorter将列的内容排序为文本而不是数字。

I am using the jQuery tablesorter2.0 plugin which is using 'parseFloat' for cells that start with a digit. The issue is that parseFloat returns very small numbers represented as 1.23e-7 as a string and is not expanding this to 0.000000123. As a result tablesorter sorts the content of the column as text instead of numerics.


**Column To Sort**
2.34
1.01
13.56
1.23e-7

**After Sort Now**
1.01
1.23e-7
13.56
2.34

**Expect**
1.23e-7
1.01
2.34
13.56


是否有一种有效的表达方式小的科学记数字作为扩展的浮点数?

Is there an efficient way of representing very small scientific notation numbers as expanded floating-point numbers?

解决方案:

tablesorter根据第一个tablesorters自动解析器确定如何对列进行排序,以便为该列中单元格的内容返回true。
如果单元格包含1.23e-7而不是默认按文本排序,因为'digit'解析器不会将其解释为数字。

tablesorter determines how to sort a column based on the first of tablesorters automatic parsers to return true for the content of a cell in that column. If the cell contained 1.23e-7 than it defaulted to sort by text because the 'digit' parser does not interpret this as a number.

所以要解决方法是,以下代码将科学记数法编号表示为tablesorter可以解释/解析为数字的字符串,从而确保对列进行数字排序。 @bitplitter - 感谢toFixed()提示。

So to workaround, the following code represents the scientific notation number as a string that tablesorter can interpret/parse as a digit and so ensures numerical sorting on the column. @bitplitter - thanks for the toFixed() tip.

var s = "1.23e-7";
// Handle exponential numbers.
if (s.match(/^[-+]?[1-9]\.[0-9]+e[-]?[1-9][0-9]*$/)) {
  s = (+s).toFixed(getPrecision(s));
}
//returns 0.000000123

// Get a nice decimal place precision for the scientific notation number.
// e.g. 1.23e-7 yields 7+2 places after the decimal point
// e.g. 4.5678e-11 yields 11+4 places after the decimal point
function getPrecision(scinum) {
  var arr = new Array();
  // Get the exponent after 'e', make it absolute.  
  arr = scinum.split('e');
  var exponent = Math.abs(arr[1]);

  // Add to it the number of digits between the '.' and the 'e'
  // to give our required precision.
  var precision = new Number(exponent);
  arr = arr[0].split('.');
  precision += arr[1].length;

  return precision;
}


推荐答案

即使OP发布了他的解决方案,我想分享一个我偶然发现的相当简单的解决方案,它基于tablesorter中的解析器源代码和JasonS给出的正则表达式另一个问题

Even though the OP has posted his solution, I'd like to share a rather simpler solution I stumbled upon, which is based on the parsers in the tablesorter source code and the regex given by JasonS on another question.

// add parser through the tablesorter addParser method 
$.tablesorter.addParser({ 
// set a unique id
id: 'scinot', 
is: function(s) { 
    return /[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/.test(s); 
}, 
format: function(s) { 
    return $.tablesorter.formatFloat(s);
}, 
type: 'numeric' 
});

它适用于我的表格,几乎所有的值都用科学记数法给出。它会自动检测(部分)并正确排序多个字段。
希望它可以帮助那些可能偶然发现这个问题的人。

It works on my tables with pretty much all values given in scientific notation. It auto-detects (the is: part) and correctly sorts multiple fields. Hope it helps others who might stumble upon this question.

这篇关于当需要0.000000123时,Javascript parseFloat'1.23e-7'给出1.23e-7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆