PDA

View Full Version : character limit



xzyfer
May 4th, 2003, 12:32 AM
ive got a dynamic text box.. var name c..
and i got some maths in the as that then send the answer to the dynamic text box c...but i oftenget the chacaters getting cut off..
is there a wat to limit how many number are displayed useing as without rounding to whole numbers..i need to set a sertai amout of decimal places..thx..

kode
May 4th, 2003, 01:26 AM
it's not that hard... ;)

// prototype
Number.prototype.limitDecimals = function(decimals) {
var str, tmp = this.toString(), dIndex = tmp.lastIndexOf(".")+1, index;
for (index=0; index<dIndex+decimals; index++) str += tmp.charAt(index);
return str;
};
// usage
Number.limitDecimals(decimals);
// example
myNumber = 7.7777;
trace(myNumber.limitDecimals(2)); // output: 7.77
=)

kode
May 4th, 2003, 03:05 AM
forget about that... as usual, i like the hard way. :P

Number.prototype.limitDecimals = function(decimals) {
this = this.toString().substr(0, this.toString().lastIndexOf(".")+1+decimals);
return Number(this);
};
that's much better. =)

senocular
May 4th, 2003, 09:56 AM
theres also


Number.prototype.limitDecimals = function(depth) {
var factor = Math.pow(10,depth);
return Math.round(this*factor)/factor;
};

which rounds to that decimal and not just chops :)

kode
May 4th, 2003, 10:46 AM
interesting... why i didn't think about that!? :P

wait!! i know the answer... i'm a rookie. :-\ :P