为什么toPrecision返回一个字符串? [英] Why does toPrecision return a String?
问题描述
查看此代码:
function testprecision(){
var isNotNumber = parseFloat('1.3')。 toPrecision(6);
alert(typeof isNotNumber); // =>字符串
}
我会预期一个数字。如果isNotNumber应该是一个真正的数字,重铸是解决方案:
alert(typeof parseFloat(isNotNumber))// = >编号
感谢您的回答。精确度并不是我所说的一个术语。它可以表示一个数字的总位数,或小数位数。荷兰的大多数人(我来自哪里)都想到小数位数的精确性。 JavaScript toPrecision方法涉及第一个表示,所以这是令人困惑的。无论如何,这个方法能够引入错误的精确度,对吗?对于第二个含义,我们必须修正,同样的(返回字符串,错误精度的可能性)。
无论如何,重新开始我的主要爱好之后,我使用我在这里收集的知识来构建一个JavaScript浮动对象。也许这对某个人来说是有用的,或者也许你们中有一个人有更好的想法?
pre $函数Float(f,nDec){
var Base = this,val;
setPrecision(nDec || 2);
set(f || 0,nDec || Base.precision);
Base.set = set;
Base.ndec = setPrecision;
/ ** public setprecision
*设置小数
*位数(小数)的数值,您希望getf为
* return。 NB:不能超过20.
*返回Float对象,所以允许方法
* chaining
* @param {Number} iPrecision
* /
函数setPrecision(iPrecision){
var ix = parseInt(iPrecision,10)|| 2;
Base.precision = ix> = 21? 20:ix;
返回基地;
}
/ ** public set
*设置对象的内部值。返回
*的Float对象,所以允许方法链接
* @param {Number} f
* @param {Number} ndec
* /
function set(f ,ndec){
val = parseFloat(f)|| 0;
if(ndec){setPrecision(ndec); }
Base.val = val;
返回基地;
$ b $ ** public get:
*返回数字值(作为浮点数)
* /
Base.get = function(){
var ndec = Math.pow(10,Base.precision),
ival = parseInt(val * ndec,10)/ ndec;
Base.val = ival;
返回Base.val;
};
/ ** public getf
*返回带精度的格式化字符串
*(参见Base.setPrecision)
*如果提供了[hx],则返回
* the float为十六进制,否则
* @param {Boolean} hx
* /
Base.getf = function(hx){
var v = Base.val.toFixed(Base。精确);
返回hx? v.toString(16):v;
};
/ ** public add
*将[f]添加到当前值(如果[f]是
* Float,否则返回当前值)
*可选地设置一个新的参数[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
*返回Float对象的内部值
*函数就像一个getter(推测)
* /
Base.toString = Base.get ;
$ / code>
用法/示例:
var xf = new Float(); // =>现值0.0
xf.set(0.86 / 0.8765,17).add(3.459);
alert(xf +'|'+ xf.getf()); // => 4.440175128351398 | 4.44017512835139800
将数字对象表示为指定的精度。
toPrecision()似乎用于格式化输出,在这种情况下,字符串是最合理的结果。它代表了最终的输出形式,不会被进一步的操作弄乱。
如果你打算为了计算原因而做一些精度截断,我倾向于乘由10 ^ n其中n是我想保留的数字,取一个整数,然后再除以相同。但这并不完美:在某些情况下,您可能会发生溢出。坦率地说,我更喜欢在服务器上进行更复杂的财务计算,我有一个货币,二进制编码的十进制或类似的数字类型。
View this code:
function testprecision(){
var isNotNumber = parseFloat('1.3').toPrecision(6);
alert(typeof isNotNumber); //=> string
}
I would have expected a number. If 'isNotNumber' should be a real number, recasting is the solution:
alert(typeof parseFloat(isNotNumber)) //=> number
[Edit] thanks for your answers. Precision is not so precise a term I conclude. It can represent the total number of digits of a number, or the number of fractional digits. Most people in the Netherlands (where I come from) think of precision in the 'number of fractional digits'-way. The javascript toPrecision method concerns the first representation, so this is confusing. Anyway, the method makes it possible to introduce 'false precision', am I right? For the second meaning we have toFixed, the same goes for that (returns string, possibility of false precision).
Anyway, having made reinventing the wheel my main hobby, I played around to construct a javascript float object, using the knowledge I gathered here. Maybe it's usefull for someone out there, or maybe one of you have better ideas?
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;
}
usage/example:
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
From the docs: "Returns a string representing the Number object to the specified precision."
toPrecision() seems intended for formatting output, in which case a string is the most reasonable outcome. It represents the final output in a form that will not be mangled by further manipulation.
If you are looking to do some truncation of precision for calculation reasons, I tend to multiply by 10^n where n is the digits I want to keep, take an integer from that and then divide again by the same. This isn't perfect though: in some situations you may invite an overflow. Frankly, I prefer to do more complex financial calculations on the server, where I have a currency, binary coded decimal or similar numeric types.
这篇关于为什么toPrecision返回一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!