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

Stopping unity from rounding numbers up?

Discussion in 'Scripting' started by johnseefeldt, Jun 4, 2014.

  1. johnseefeldt

    johnseefeldt

    Joined:
    Jun 4, 2014
    Posts:
    1
    I'm trying to simulate currency and require individual cents to be added. Instead of adding 0.01 to the total it rounds up to 1. How can I stop this from happening?

    Thank you,

    ( The first script listens for a key press and adds to the static variable of the second script. )

    Code (JavaScript):
    1.  
    2. var insectToAdd = 0.01;
    3.  
    4.  
    5. var plantToAdd = 0.01;
    6.  
    7.  
    8.  
    9.  
    10. function Update () {
    11.        
    12. if (Input.GetKeyDown ("z") && Input.GetKey ("i"))
    13. CoinInsectManager.insect += insectToAdd;
    14.  
    15.  
    16.  
    17. if (Input.GetKeyDown ("z") && Input.GetKey ("p"))
    18. CoinPlantManager.plant += plantToAdd;
    19.  
    20.  
    21.  
    22.     }
    23.  

    Code (JavaScript):
    1.  
    2. static var insect = 0.00;
    3.  
    4.  
    5. function Update ()
    6.  
    7. {
    8.  
    9. guiText.text =  "Worm Fund: $" + insect;
    10.  
    11. }
    12.  
     
  2. Nerevar

    Nerevar

    Joined:
    Mar 15, 2013
    Posts:
    11
    Well I tried your code and it worked just fine for me ...
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    1) type your variables, there are multiple numeric types available in mono, you should be typing them to be certain you have them.

    2) not sure if unityscript defaults to float or double. I know C# defaults to double if you have a decimal point in your value.

    3) decimal type is best for currency as it is stored in base 10 and doesn't have the binary <-> decimal conversion errors (0.1 in binary isn't actually 0.1 in the few bits available. If you sum 0.1 together 10 times you'll get like 0.9999... but with decimal you wont)
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    The variables are typed; as long as you supply a value, they are typed based on the value (e.g. using an int types the variable as an int, using a float types it as a float, etc.).

    Float.

    Yep, or you could use ints for all the math and format it for display as desired. Either way, using floats for this code is a bad idea.

    --Eric
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    @Eric5h5 - actually no, they aren't typed in the manner I meant. The value going in isn't obvious of its type.

    var a = 1;
    var b = 1.0;

    it's not directly obvious what is what here... especially when you consider that this could be either unityscript OR C# (they're identical in this instance).

    If it was instead:

    var a:int = 1;
    var b:float = 1.0;

    obvious

    or

    var a = 1;
    var b = 1.0f;

    also a little more obvious (again, not sure if no decimal point is int in unityscript... I personally don't use the language, nor care to learn it... I already have 20+ languages in my head)

    I mean of course they're typed in the sense the compiler will type them, they're not going to be untyped/dynamic or anything. It's just that you can't READ the type in any obvious manner.

    implicit typing with say:

    var obj = new SomeClass();

    this is obvious what the implicit type is. But not in the OPs code. And it's a good habit to start writing readable code.