gpt4 book ai didi

javascript - 为什么 toPrecision 返回一个字符串?

转载 作者:行者123 更新时间:2023-12-02 05:21:57 25 4
gpt4 key购买 nike

查看这段代码:

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

我本以为会有一个数字。如果 'isNotNumber' 应该是实数,则重铸是解决方案:

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

[编辑]谢谢你的回答。我得出的结论是,精度并不是那么精确。它可以表示一个数字的总位数,或者小数位数。荷兰(我来自荷兰)的大多数人都以“小数位数”的方式来考虑精度。 javascript toPrecision 方法涉及第一个表示,所以这很困惑。无论如何,该方法可以引入“错误精度”,对吗?对于我们必须固定的第二个含义,同样适用于它(返回字符串,错误精度的可能性)。

无论如何,在把重新发明轮子作为我的主要爱好之后,我利用在这里收集的知识尝试构建一个 javascript float 对象。也许它对外面的人有用,或者你们中的某个人有更好的想法?

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() 似乎用于格式化输出,在这种情况下,字符串是最合理的结果。它以一种不会被进一步操作破坏的形式表示最终输出。

如果您出于计算原因希望对精度进行一些 chop ,我倾向于乘以 10^n,其中 n 是我想要保留的数字,从中取一个整数,然后再除以相同的整数。但这并不完美:在某些情况下,您可能会导致溢出。坦率地说,我更喜欢在服务器上进行更复杂的财务计算,我在服务器上有货币、二进制编码的十进制或类似的数字类型。

关于javascript - 为什么 toPrecision 返回一个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/503716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com