Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Int to 0. Float

Discussion in 'Scripting' started by andrew-furlyk, May 15, 2017.

  1. andrew-furlyk

    andrew-furlyk

    Joined:
    Jun 7, 2014
    Posts:
    27
    how would I go around about making an int to a float, so for example, i have 1 as int and I want it to be a 0.1 float.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Multiply your int by .1 and cast it into a float? You might have to give more info. Does 15 become 1.5 or .15? Is 145 become 1.45 or 14.5 or .145?

    It could be as simple as multiplying by the correct value, or just inserting a decimal into a string.
     
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    1 as a float would be 1.0, not 0.1

    if you want to convert 1 to 0.1, and 2 to 0.2 then multiply the int by 0.1f, or divide it by 10
    But what happens when you get to an integer of 10?
     
    andrew-furlyk likes this.
  4. andrew-furlyk

    andrew-furlyk

    Joined:
    Jun 7, 2014
    Posts:
    27
    I just needed to multiply the int by 0.1f. thanks
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Code (CSharp):
    1. int i = 1;
    2.  
    3. void Start()
    4. {
    5. float f = i * 0.1;
    6. Debug.Log("f : " + f);
    7. }