Search Unity

Force a float into an integer value

Discussion in 'Scripting' started by TortoRacoon, Jun 17, 2019.

  1. TortoRacoon

    TortoRacoon

    Joined:
    Apr 17, 2019
    Posts:
    61
    Very easy and quick question.

    I want characters to increment a 10% of their current health when leveling up. Sometimes that number is not an integer (I need them to be integer for something very important). Can I make it take the 10% of the number and force that number to take an equivalent as an int number?

    Thanks in advance
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Code (CSharp):
    1. float aFloat = 5.125f;
    2. int aInt = (int)aFloat;
     
    TortoRacoon likes this.
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Code (csharp):
    1.  
    2. float aFloat = 5.615f;
    3. int aInt = Mathf.FloorToInt(aFloat); // 5
    4. aInt = Mathf.RoundToInt(aFloat); // 6
    5. aInt = Mathf.CeilToInt(aFloat); // 6
    6.  
     
    Kwinten likes this.