Thread: Convert double to int.

Results 1 to 7 of 7
  1. #1 Convert double to int. 
    Donator

    Join Date
    Jul 2009
    Posts
    1,452
    Thanks given
    27
    Thanks received
    13
    Rep Power
    69
    Is there any way to convert a double to an int?

    I made a code that should calculate the hits of a player, and then return them as ints having no decimals, because the hits are not having decimals.

    Is there a way to do this?

    This is my current code:
    Code:
    public int effectiveStrength() {
    	/**
    	*	Calculating the first step in the runescape max hit formula.
    	*	Prayer, Potions and "otherBonus" will be added later.
    	*/
    	
    		int potionBonus = 0;
    		int prayerBonus = 0;
    		int otherBonus = 0;
    		
    		double effectiveStrength2 = (((getLevelForXP(playerXP[2])+potionBonus)*prayerBonus*otherBonus)+playerBonus[getBonusFromFightType]);
    		
    		int result = Math.floor((int)effectiveStrength2);
    		
    		return result;
    	}
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Feb 2009
    Age
    27
    Posts
    2,861
    Thanks given
    127
    Thanks received
    226
    Rep Power
    700
    Post your current code.
    Reply With Quote  
     

  3. #3  
    Donator

    Join Date
    Jul 2009
    Posts
    1,452
    Thanks given
    27
    Thanks received
    13
    Rep Power
    69
    Quote Originally Posted by Jarba View Post
    Post your current code.
    just did
    Reply With Quote  
     

  4. #4  
    Renown Programmer
    Method's Avatar
    Join Date
    Feb 2009
    Posts
    1,455
    Thanks given
    0
    Thanks received
    845
    Rep Power
    3019
    You already cast a double to an int in your code...
    :-)
    Reply With Quote  
     

  5. #5  
    Registered Member

    Join Date
    Feb 2009
    Age
    27
    Posts
    2,861
    Thanks given
    127
    Thanks received
    226
    Rep Power
    700
    Mads, what's the error?
    Reply With Quote  
     

  6. #6  
    Registered Member
    PSNB's Avatar
    Join Date
    Aug 2009
    Posts
    885
    Thanks given
    8
    Thanks received
    103
    Rep Power
    590
    Math.floor(...) is a double. You've got to cast that to an integer, not 'effectiveStrength2'.

    Code:
    public int effectiveStrength() {
    	/**
    	*	Calculating the first step in the runescape max hit formula.
    	*	Prayer, Potions and "otherBonus" will be added later.
    	*/
    	
    		int potionBonus = 0;
    		int prayerBonus = 0;
    		int otherBonus = 0;
    		
    		double effectiveStrength2 = (((getLevelForXP(playerXP[2])+potionBonus)*prayerBonus*otherBonus)+playerBonus[getBonusFromFightType]);
    		
    		int result = (int)Math.floor(effectiveStrength2);
    		
    		return result;
    	}
    Reply With Quote  
     

  7. #7  
    Community Veteran

    mige5's Avatar
    Join Date
    Aug 2008
    Posts
    5,528
    Thanks given
    573
    Thanks received
    1,410
    Rep Power
    2114
    heres an example how to do it:
    Code:
    	 	  int number1;  
    	 	  double number2 = 1.54;
    	 	  number1 = (int)number2;
    Number of page #1 releases with most views & posts: (Updated: 2023)
    RS2 server section: 1
    RS2 client section: 2
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

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