Round numbers

Discussion in 'JavaScript' started by XandroZ, May 13, 2007.

  1. #1
    I need to round a result number,by precision;

    Example:
    Math : y=x/1000000; x=5 => y=0.000005,
    but if I do this in java script y=0.00000500001;(not right) ;how do I round a equation by precision to have a math result
     
    XandroZ, May 13, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    ajsa52, May 13, 2007 IP
    XandroZ likes this.
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    
    <script language="javascript">
    function php_like_round( num, pre )
    {
     	var temp = '1';
    
     	for( i = 0 ; i < pre; i++ )
     	{
    		temp += '0';	
    	}
    	return Math.round( num * temp )/ temp ;
    }
    document.write( php_like_round( '4039.23344534534', '2' ) );
    </script>
    
    HTML:
    that works .....
     
    krakjoe, May 14, 2007 IP
  4. XandroZ

    XandroZ Peon

    Messages:
    395
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thx very much
     
    XandroZ, May 14, 2007 IP