A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Rounding Values up to 2Decimal Places?

  1. #1
    -
    Join Date
    Feb 2006
    Posts
    74

    Rounding Values up to 2Decimal Places?

    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.

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    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

  3. #3
    -
    Join Date
    Feb 2006
    Posts
    74
    cheers explains it well

  4. #4
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    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);
    }
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center