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. Dismiss Notice

Question how to normalize a range of values to 0 1 range

Discussion in 'Scripting' started by Arcan-Studios, Sep 20, 2020.

  1. Arcan-Studios

    Arcan-Studios

    Joined:
    Mar 12, 2015
    Posts:
    58
    Hi i need to know how can i achiveve a normalize for ranged values for example

    i have range 2304 to 0

    and i want to know 1743 value in the range of 0 to 1
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    float normal = 1743 / 2304;

    I can never remember if you need to cast ints to floats in operations like this so probably safer to to avoid decimals being truncated in the division of ints.

    float normal = (float)1743 / (float)2304;

    or for short

    float normal = 1743f / 2304f;

    Can someone clarify this?
     
    Arcan-Studios likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
  4. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    Arcan-Studios likes this.
  5. Arcan-Studios

    Arcan-Studios

    Joined:
    Mar 12, 2015
    Posts:
    58
    thanks very much