Search Unity

Going insane. Simple problem I can't solve.

Discussion in 'Scripting' started by MindGem, Apr 29, 2018.

  1. MindGem

    MindGem

    Joined:
    May 11, 2017
    Posts:
    84
    Hiya.

    I just want 2 decimals for my number but I can't get it to work.

    stockPrice:169.30
    Dividend:8.25

    Now I want to calc how much dividend I'd get if the stock price were 100 flat.
    So I figure I do 8.25/169.30 = 0.0487...
    then 100*0.0487... = 4.87.

    Now 4.87 is the result I want.

    I try;
    damnResult = (Mathf.Round(dividend/stockPrice)*100)/100;
    doesnt work, so I break it down to see where I get confused and do 2 separate calcs
    but it doenst work anyways. I know this is a simple problem, what am I doing wrong?
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    To get two decimals, you need to do this:
    Mathf.Round(num * 100) / 100
    So your whole code should look like this:
    Mathf.Round(((Mathf.Round(dividend * 100) / 100) / (Mathf.Round(stockPrice * 100) / 100) * 100) * 100) / 100
    or, if you turn the double decimal thing into a method:
    twoDec(twoDec(dividend) / twoDec(stockPrice) * 100)

    Also, if you simplify it, you can do this:
    Mathf.Round(Mathf.Round(dividend * 100) / Mathf.Round(stockPrice / 100) * 10000) / 100
     
  3. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    use 100f, the float version of 100 (edit: ops maybe not)
     
    Last edited: Apr 29, 2018
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    That wouldn't make a difference, as dividend and stockPrice are floats, rounding them results in a float, and dividing a float with an integer also works
     
    rakkarage likes this.
  5. MindGem

    MindGem

    Joined:
    May 11, 2017
    Posts:
    84
    Thx for the replies guys.

    I'm still stuck though, I get the result from your code Gorbit99: 41250. I don't even know how it got that result.

    public float price = 169,70;
    public float dividend = 8.25;
    Debug.Log(Mathf.Round(Mathf.Round(dividend * 100) / Mathf.Round(price / 100) * 10000) / 100);

    Result: 4125000

    Any ideas?
     
  6. MindGem

    MindGem

    Joined:
    May 11, 2017
    Posts:
    84
    Wait wait!

    I just tested the last line of code you had and stepped away for a while.
    The first code you had worked as intended. Thank you very much. This is a great forum with lots of helpful people.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just for the record, if this is for display only to 2 digits, it would maybe even be better to use the decimal type for the math, and format the string to show only 2 places.
     
  8. Fab4

    Fab4

    Joined:
    Sep 30, 2012
    Posts:
    114
    169,70 is actually 169700 and not 169.7
     
  9. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    No, that's actually an "identifier expected" error
     
    Fab4 likes this.
  10. Fab4

    Fab4

    Joined:
    Sep 30, 2012
    Posts:
    114
    My post was crap anyway, because the 169.7 are in the denominator
    x]
     
  11. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Btw, I messed up, it should be price * 100 instead of price / 100, that should remove around 6 zeroes from the result
     
  12. MindGem

    MindGem

    Joined:
    May 11, 2017
    Posts:
    84
    How do I do that then?
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Doug_B likes this.
  14. MindGem

    MindGem

    Joined:
    May 11, 2017
    Posts:
    84
    Thank you so much methos5k.

    Gahdangit I've been sitting with this problem for hours and hours and looking at solutions on the web but they all seem to relate to another type of C# with Mathf.round(num,2) to get two decimals and it wont work for me.

    But this thing you showed me works so finally I can move on to other things. thx :)
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem. Perhaps other examples were trying to get a rounded value as a float (or some numerical value), whereas this solution is changing the value when it's represented as a string.

    :) different options for different tasks.

    Take care.