|
float integer
function Round2Nearest(n) {
//
var floatnumber = Number(n);
var Rounded = Math.round(floatnumber);
var NextInt = Math.ceil(floatnumber);
var PrevInt = Math.floor(floatnumber);
//
if ( (Math.abs(floatnumber)+ 0.5) != NextInt) {
//
trace(floatnumber add " >NextInt:" add NextInt);
trace(floatnumber add " >PrevInt: " add PrevInt);
var diff2next = (NextInt-floatnumber);
var diff2prev = (floatnumber-PrevInt);
trace("al prossimo intero mancano: " add diff2next);
trace("all'intero precedente mancano: " add diff2next);
//
var Major = Math.max(diff2prev, diff2next);
var IsPos = (floatnumber>0);
//
if (Major == diff2prev) {
trace("floatnumber è da chiudere all'intero precedente");
return IsPos ? PrevInt : NextInt;
}
else {
trace("floatnumber è da chiudere all'intero successivo");
return IsPos ? NextInt : PrevInt;
}
//
}
else {
trace("case: 0,5 but non 0,5 periodico... ("add floatnumber add")");
return floatnumber;
}
}
//
//
//
//
//
//Usage:
trace(Round2Nearest(0.000001));
trace(Round2Nearest(10.89));
trace(Round2Nearest(0.09));
trace(Round2Nearest(0.5));
trace(Round2Nearest(0.555555));
trace(Round2Nearest(0.5555551));
//
this.onEnterFrame = function() {
trace(Round2Nearest(Math.random()));
};
stop;
Posted by: fabio vergani | website http:\\www.kora.it |
function addCommas(theNumber){
numString = theNumber + ""; //convert # to string
newString = ""; //return string
index = 1; // string index
trip = 0; // trip, as in triple.
while(numString.length >= index){
if (trip!=3){ //haven't reached 3 chars yet
newString = numString.charAt(numString.length - index) + newString; //add char
index++;
trip++;
}
else{ //reached 3 chars, add comma
newString = "," + newString;
trip=0;
}
}
return (newString);
}Posted by: djb | website http://www.webdev.nu |
/** * Formats the given Number with commas */ public static function formatNumberWithCommas(num) { var strNum:String = num+""; if (strNum.length < 4) return strNum; return formatNumberWithCommas(strNum.slice(0, -3))+","+strNum.slice(-3); } Posted by: vineet_sc | website http://www.vineetsc.com |
//This one could be handy to format values to a currency format.
Math.__proto__.toMoney = function (num){
valor = String (Math.round(num * 100) / 100);
dot = valor.indexOf(".");
money = "$";
if(dot == -1){
valor += ".0";
}
temp = valor.split(".");
addDecimals = 2 - temp[1].length;
for(i=1; i<= addDecimals; i++){
valor += "0";
}
return money+valor;
};
a = 0.5;
b = 0.8;
trace(z = Math.moneyFormat(5));//if you trace thism, z will display "$5.00"
trace(z = Math.moneyFormat(a + b));//if you trace thism, z will display "$1.30"
Posted by: LordAlex Leon | website http://www.lordalex.com |
//format a number into specified number of decimal places
Math.formatDecimals = function (num, digits) {
//if no decimal places needed, we're done
if (digits <= 0) {
return Math.round(num);
}
//round the number to specified decimal places
//e.g. 12.3456 to 3 digits (12.346) -> mult. by 1000, round, div. by 1000
var tenToPower = Math.pow(10, digits);
var cropped = String(Math.round(num * tenToPower) / tenToPower);
//add decimal point if missing
if (cropped.indexOf(".") == -1) {
cropped += ".0"; //e.g. 5 -> 5.0 (at least one zero is needed)
}
//finally, force correct number of zeroes; add some if necessary
var halves = cropped.split("."); //grab numbers to the right of the decimal
//compare digits in right half of string to digits wanted
var zerosNeeded = digits - halves[1].length; //number of zeros to add
for (var i=1; i <= zerosNeeded; i++) {
cropped += "0";
}
return(cropped);
} //Robert Penner May 2001 - source@robertpenner.com
Posted by: Robert Penner | website http://www.robertpenner.com |

