I came across different posts that talk about alternatives to toFixed, which doesn't work in Safari. However, I didn't find a solution that would really work for me - I don't want to hardcode any decimal places, I want to pass it as an argument to a function. And I found a function on another site, which supposed to work on both Explorer and Safari; the thing is that it does work in Explorer (returns '300.00'), but in Safari I get this weird result: '300function pad(s) { s= s || "."; return s.length > n ? s : pad(s+"0");'
This is the function definition:
This is how I call the function:
This is the function definition:
Code:
Number.prototype.toDecimals=function(n){
n=(isNaN(n))?
2:
n;
var nT=Math.pow(10,n);
function pad(s){
s=s||'.';
return (s.length>n)?
s:
pad(s+'0');
}
return (isNaN(this))?
this:
(new String(
Math.round(this*nT)/nT
)).replace(/(\.\d*)?$/,pad);
}
This is how I call the function:
Code:
var myNumber=300, formattedNumber=myNumber.toDecimals(3);