Question on usage of javascript's toFixed() function
I was looking for a JavaScript function to round a number to a specified amount of decimal places & I found this page: http://www.kadimi.com/en/round-float-349
That page includes a section that uses prototyping & the toFixed() function..... however I also found this page: http://freewebdesigntutorials.com/javaScriptTutorials/jsNumberObject/toFixedMethod.htm (see last example) & this uses the toFixed function straight up & appears to do the same thing.
I'm not a big javascript person so apologies if this is a stupid question, but what's the difference between them?
Here they are for clarity..
With prototype:
if (!Number.toFixed) {
Number.prototype.toFixed=function(n){
return Math.round(this*Math.pow(10, n)) / Math.pow(10, n);
}
}
// example:
floating_number = 123.45687;
decimal_points = 2;
window.alert(floating_number.toFixed(decimal_points));
Standard:
var numex = 3.1415926535;
alert( numex.toFixed(5) );
I also tried out that first batch of code with this function..
function round_float(number,places){
if (!number.toFixed) {
number.prototype.toFixed=function(places){
return Math.round(this*Math.pow(10, places)) / Math.pow(10, places);
}
} else {
alert('bad');
}
}
It went into the "bad" alert section..... I'm guessing that was caused by the false response by toFixed; any idea why this function is written li开发者_Go百科ke that then?
Simple. Not all browsers have a toFixed
so in the first example it provides a safety net. The alternative means the browser does have a toFixed
which is not "bad" but "normal".
I'm not sure why you have the alert('bad')
in your else block; all that really happens in the "bad" case, is that the number
variable already has a member called toFixed
defined (which evaluates to true when coerced to a boolean - always the case for a function).
That's not necessarily a bad thing, since this is what you want the end result to be?! The code seems to be defining a version if toFixed
if there isn't already a native* implementation. The alert fires when there is native* support.
*(Well, either native to the browser or already added to the prototype by a JS library. Either way, it's already there so no more work is needed.)
I would try something like this:
if (!Number.toFixed){
Number.prototype.toFixed = function(n){
return Math.round(this * Math.pow(10, n)) / Math.pow(10, n);
}
}
var num = 123.4567;
$('body').append(num.toFixed(3));
Perform the "Existence check" before you need it, and not within the function. (Confirmed to work)
I am using function for javascript toFixed() functions. In my project QA tested for unit price For example it will be 79.9989 so if i used toFixed(2) result was 80.00. In my QA does not wants toFixed with 80.00 they want 79.99
Some cases : Before after tofixed(2) 78.896 78.89 78.996 78.99
So i used my function like this:
function customtoFixed(num){
if(num % 1 != 0){
var split_nums = (''+num).match(/(\d+)(\.\d+)?/);
console.log(split_nums);
if(split_nums[2]>.995){
return (+split_nums[1]) + (+0.99);
} else {
return num.toFixed(2);
}
} else {
return num.toFixed(2);
}
}
精彩评论