I have a question regarding toFixed() function. If I have a float e.g. - 3.123123423 and 0. How can I output it to input box with toFixed(2) so the inputbox values would be 3.12 and 0. I mean if the value is integer I want output it without trailing .00 :)
问题:
回答1:
As there is no difference between integers and floats in JavaScript, here is my quick'n'dirty approach:
theNumber.toFixed(2).replace(".00", "");
Or something generic:
myToFixed = function (n, digits) {
digits = digits || 0;
return n.toFixed(digits).replace(new RegExp("\\.0{" + digits + "}"), "");
}
myToFixed(32.1212, 2) --> "32.12"
myToFixed(32.1212, 0) --> "32"
myToFixed(32, 2) --> "32"
myToFixed(32.1, 2) --> "32.10"
回答2:
You don't need Math.round():
var toFixed = function(val, n) {
var result = val.toFixed(n);
if (result == val)
return val.toString();
return result;
}
toFixed(3.123, 2) --> "3.12"
toFixed(3, 2) --> "3"
回答3:
function toFixed0d(x, d)
{
if(Math.round(x) == x)
{
return x.toString();
}else
{
return x.toFixed(d);
}
}
toFixed0d(3.123123423, 2) = "3.12"
toFixed0d(0, 2) = "0"
回答4:
Note that toFixed() is broken in IE and generally not to be relied on. Annoying but accurately-rounded replacement functions given there.
回答5:
Number.toFixed produces some mathematically bizarre results and gives you the fractional digits you asked for, whether you need them or not.
Math.round is mathematically correct, but doesn't allow you to specify a precision.
Here's a function that's both mathematically correct (disregarding floating point errors) and that gives you what you want.
var roundToPlusInfinity = function(n, fd) {
var scale = Math.pow(10, fd),
rounded = fd ? Math.floor((n * scale + 0.5)) / scale :
Math.round(n);
return rounded.toString();
};
The algorithm is: scale up so that the digits you want are to the left of the decimal point, add .5 (so that >= .5 in the first insignificant place causes an increment of the integer portion), truncate (not round!) and scale back down. There's the potential for floating point errors to creep in, but it's a sight better than the wretched toFixed function.
Testing it out:
roundToPlusInfinity(5.53335, 4); // "5.3334"
roundToPlusInfinity(-9999.95, 1); // "-9999.9"
roundToPlusInfinity(3.00015, 3); // "3"
Because of the possibility of floating point error, the scale down might give a result like "3.00000000001", which is annoying. Something like this should take care of it.
var trimFraction = function(s, fd) {
var splitPoint = s.indexOf('.');
if (splitPoint === -1) {
return s;
}
var f = s.substring(splitPoint + 1, s.length),
i = s.substring(0, splitPoint);
if (f.length > fd) {
f = f.substring(0, fd).replace(/0+$/, "");
}
if (f) {
return i + '.' + f;
}
return i;
};
trimFraction("3.1000000001", 2); // "3.1"
You haven't made clear the rounding algorithm you want. Round towards plus infinity is the rounding you learnt at school where you round >= .5 up (i.e. towards positive infinity). I believe that in accounting, the correct rounding algorithm (outside Switzerland and Argentina at least) is away from zero. Simple:
var roundAwayFromZero = function(n, fd) {
var ret = roundToPlusInfinity(Math.abs(n), fd);
return (n < 0) ? '-' + ret : ret;
};
roundAwayFromZero(-9999.95, 1); // "-10000"
If the results are for human presentation, Intl.NumberFormat is a possibility, since you can specify both the minimum and maximum number of fractional digits. At the time of writing it's not exactly widely supported and, assuming that Chrome implements the specification correctly, uses the same impenetrable and plain wrong truncating algorithm as toFixed. Interestingly, the "currency" style uses roundAwayFromZero (or round to a half if the currency code is "CHF", which is a nice touch).
回答6:
Here is a function I came up with after thinking about this same problem for a little while:
function toFixed (number, n) {
return parseFloat(number.toFixed(n));
}
As per this question, this function will return as follows:
toFixed(3.123123423, 2);
> 3.12
toFixed(0, 2);
> 0
toFixed(3.1, 2);
> 3.1
toFixed(3.005, 2);
> 3
toFixed(3.005, 3);
> 3.005
toFixed(3.0500, 4);
> 3.05
Note that the toFixed
method of the JavaScript's Number
object actually returns a String
. The above toFixed
function will return a Number
. If you'd rather have a String
then use the following instead:
function toFixed (number, n) {
return parseFloat(number.toFixed(n)).toString();
}