If flash outputs the value 176.776695296637 in a trace, Math.round() makes it 177 how can i round it up to 2 Decimal Places? 176.78
Last edited by Owenbooty; 02-21-2007 at 11:12 AM.
Code: num = Math.PI; trace(num); // 3.14159265358979 value0= Math.round(num); trace(value0); // 0 decimal place - result 3 value1= Math.round((num)*10)/10; trace(value1); // 1 decimal place - result 3.1 value2= Math.round((num)*100)/100; trace(value2); // 2 decimal place - result 3.14 value4= Math.round((num)*10000)/10000; trace(value4); // 4 decimal place - result 3.1416
num = Math.PI; trace(num); // 3.14159265358979 value0= Math.round(num); trace(value0); // 0 decimal place - result 3 value1= Math.round((num)*10)/10; trace(value1); // 1 decimal place - result 3.1 value2= Math.round((num)*100)/100; trace(value2); // 2 decimal place - result 3.14 value4= Math.round((num)*10000)/10000; trace(value4); // 4 decimal place - result 3.1416
cheers explains it well
and using dog's tehcnique, you can make it into a function....for convenience Code: trace(roundNumber(1.22567890, 5)); //will return the value to 5 decimal places function roundNumber(num:Number, noPlaces:Number):Number { return Math.round(num * Math.pow(10, noPlaces))/Math.pow(10, noPlaces); }
trace(roundNumber(1.22567890, 5)); //will return the value to 5 decimal places function roundNumber(num:Number, noPlaces:Number):Number { return Math.round(num * Math.pow(10, noPlaces))/Math.pow(10, noPlaces); }
Flashmatics | Flashmatics Library (free downloads) ----------------------------------------------------- Latest Projects: The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience ----------------------------------------------------- Flash Certified Developer
Forum Rules