I think this has to do with string anyways, or length…
Say i have a function that calculates a nubmer, it outputs a dollar value, so i
lets say the output should be $1.40 , it would output $1.4, that looks a bit funny, so is there a way to check the length after the decimal, and if it’s only 1 char long, to add a 0?
system
April 6, 2004, 4:01pm
2
Try this:
Number.prototype.toFixed = function (n) {
var s = Math.round(this * Math.pow(10, n)).toString();
return (s.substr(0, s.length - n)+"."+s.substr(s.length - n));
}
system
April 6, 2004, 4:31pm
3
how would i work that into this:
penT.restrict = "0-9";
nicT.restrict = "0-9";
dimT.restrict = "0-9";
quaT.restrict = "0-9";
button1.onPress = function() {
var pT = pennies.length ? pennies*.01 : _root.penT.text = 0;
var nT = nickles.length ? nickles*.05 : _root.nicT.text = 0;;
var dT = dimes.length ? dimes*.1 : _root.dimT.text = 0;
var qT = quarters.length ? quarters*.25 : _root.quaT.text = 0;
var tT = pT+nT+dT+qT
total.text = "total: $"+tT;
}
i tried
penT.restrict = "0-9";
nicT.restrict = "0-9";
dimT.restrict = "0-9";
quaT.restrict = "0-9";
button1.onPress = function() {
var pT = pennies.length ? pennies*.01 : _root.penT.text = 0;
var nT = nickles.length ? nickles*.05 : _root.nicT.text = 0;;
var dT = dimes.length ? dimes*.1 : _root.dimT.text = 0;
var qT = quarters.length ? quarters*.25 : _root.quaT.text = 0;
var tT = pT+nT+dT+qT
var s = Math.round(tT * Math.pow(10, n)).toString();
return (s.substr(0, s.length - n)+"."+s.substr(s.length - n));
total.text = "total: $"+s;
}
but that is incorrect.
system
April 6, 2004, 6:49pm
4
penT.restrict = "0-9";
nicT.restrict = "0-9";
dimT.restrict = "0-9";
quaT.restrict = "0-9";
button1.onPress = function() {
var pT = pennies.length ? pennies*.01 : _root.penT.text = 0;
var nT = nickles.length ? nickles*.05 : _root.nicT.text = 0;;
var dT = dimes.length ? dimes*.1 : _root.dimT.text = 0;
var qT = quarters.length ? quarters*.25 : _root.quaT.text = 0;
var tT = pT+nT+dT+qT
n=2
var s = Math.round(tT * Math.pow(10, n)).toString();
s= (s.substr(0, s.length - n)+"."+s.substr(s.length - n));
total.text = "total: $"+s;
}
system
April 6, 2004, 7:25pm
5
thanks, i see what i did wrong
system
April 7, 2004, 10:39am
6
You should take advantage of the fact it’s a prototype…
Number.prototype.toFixed = function (n) {
var s = Math.round(this * Math.pow(10, n)).toString();
return (s.substr(0, s.length - n)+"."+s.substr(s.length - n));
}
penT.restrict = "0-9";
nicT.restrict = "0-9";
dimT.restrict = "0-9";
quaT.restrict = "0-9";
button1.onPress = function() {
var pT = pennies.length ? pennies*.01 : _root.penT.text = 0;
var nT = nickles.length ? nickles*.05 : _root.nicT.text = 0;;
var dT = dimes.length ? dimes*.1 : _root.dimT.text = 0;
var qT = quarters.length ? quarters*.25 : _root.quaT.text = 0;
var tT = pT+nT+dT+qT
total.text = "total: $"+tT.toFixed(2);
}