JavaScript parseFloat '1.23e-7'当需要 0.000000123 时给出 1.23e-7
parseFloat(1.51e-6);
// returns 0.00000151
parseFloat(1.23e-7);
// returns 1.23e-7
// required 0.000000123
我正在对包含各种浮点数的表列进行排序,其中一些浮点数以科学计数法表示。
我正在使用 jQuery tablesorter2.0 插件,该插件对以数字开头的单元格使用“parseFloat”。 问题是 parseFloat 返回非常小的数字,表示为 1.23e-7 作为字符串,并且不会将其扩展为 0.000000123。 结果,表排序器将列的内容排序为文本而不是数字。
**要排序的列** 2.34 1.01 13.56 1.23e-7 **立即排序后** 1.01 1.23e-7 13.56 2.34 **预计** 1.23e-7 1.01 2.34 13.56
是否有一种有效的方法将非常小的科学记数法数字表示为扩展浮点数?
解决方案:
tablesorter 确定如何根据第一个 tablesorters 自动解析器对列进行排序,以对该列中单元格的内容返回 true。 如果单元格包含 1.23e-7,则默认按文本排序,因为“数字”解析器不会将其解释为数字。
因此,为了解决此问题,以下代码将科学记数法数字表示为字符串,表排序器可以将其解释/解析为数字,从而确保对列进行数字排序。 @bitplitter - 感谢 toFixed() 提示。
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;
}
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.
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?
Solution:
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.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
toFixed()
而不是parseFloat()
按照您想要的方式格式化数字。例如
(1.23e-7).toFixed(9)
将呈现为0.000000123
为了能够使用默认字符串比较对这些进行排序,请确保为所有这些添加前缀与零并使它们的大小相同,这样小数点就会对齐。
您可以使用 padLeft 扩展字符串对象,如下所示:
现在您可以调用
((1.23e-7).toFixed(9)).padLeft(15,'0')
You can use
toFixed()
instead ofparseFloat()
to format the numbers the way you want.For example
(1.23e-7).toFixed(9)
will render as0.000000123
To be able to sort these with sorts default string comparison, make sure to prefix all of them with zeroes and make them all the same size so the decimal dots wil line up.
You can extend the string object with padLeft like this:
Now you can call
((1.23e-7).toFixed(9)).padLeft(15,'0')
尽管OP已经发布了他的解决方案,但我想分享一个我偶然发现的相当简单的解决方案,该解决方案基于表排序器中的解析器 源代码 和 JasonS 在
它适用于我的表格,几乎所有值都以科学记数法给出。它会自动检测(
is:
部分)并正确对多个字段进行排序。希望它可以帮助其他可能偶然发现这个问题的人。
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.
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.
问题不在于parseFloat,而在于默认使用字符串比较的sort。尝试强制进行数字比较:
the problem is not parseFloat, but sort that uses string comparison by default. Try enforcing numeric comparison: