Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how do i make my code only display 1 or 2 numbers after the decimal?

Discussion in 'Scripting' started by Sylvir, Nov 25, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    [Solved Thank you!]

    Here is the code that i have currently...

    Code (CSharp):
    1. //This is in the update function
    2.  
    3. fishingLevelText.text = ("" + GameController.control.requieredFishingXP /GameController.control.currentFishingXP);
    this works great, but it displays the percentage like .. 5.987278123 % for example. I would like it to just say 5.9 % or 5.98 %


    I thought i just had to add .toString("F2") but that wasnt seeming to work either.
     
    Last edited: Nov 30, 2015
  2. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396

    edit: I had those in the wrong order for the division.. i fixed that, and would like it to show .85 percent of the level or whatever thanks!
     
    TheBoringNova likes this.
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Code (CSharp):
    1. String.Format("{0:0.00}", value);
    Is how you format to 2 decimal places. In your case, I would also multiply value by 100 to get the actual percent:

    Code (CSharp):
    1. fishingLevelText.text = ("" + GameController.control.requieredFishingXP /GameController.control.currentFishingXP);
    2.  
    3. var ctrl = GameController.control;
    4. float value = (ctrl.requiredFishingXP / ctrl.currentFishingXP) * 100f;
    5.  
    6. fishingLevelText.text = String.Format("{0:0.00}", value) + " %";
     
    Vendoza, TheBoringNova and Kiwasi like this.
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    could also just run
    Code (CSharp):
    1. myInt.ToString("0.00");
    on the int to convert it to a string representation of the int with only 2 decimal spaces.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    @BenZed 's code: If you're going to use string.Format, may as well avoid the extra concatenation. Also, use # instead of 0 for the first digit - it'll include leading digits as needed, where 0 just does one digit IIRC.
    Code (csharp):
    1. fishingLevelText.text = string.Format("{0:#.00} %", value);
    Finally, if ctrl.requiredFishingXP and ctrl.currentFishingXP are ints, then BenZed's code will always show you 0% - it divides them as ints (resulting in 0) and only after that becomes a float when multiplied by 100f (but 0 * 100f = 0f). You need to cast at least one to a float before the operation:
    Code (csharp):
    1. float value = ( (float)ctrl.requiredFishingXP / ctrl.currentFishingXP) * 100f;
     
    BenZed likes this.
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    ToString("F2") works fine.

    --Eric
     
  7. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    they are float values though not ints. i got it showing the correct percent now but i am still having issues working out how to get them to show only the percentage and not a bunch of decilmal places.


    Code (CSharp):
    1. fishingLevelText.text = ("" +  GameController.control.currentFishingXP /GameController.control.requieredFishingXP * 100 + "%");
    both variables are float values.
     
  8. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    when i try that its saying i can do that to a string and float.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You need to use correct syntax. Since I don't know what your code is, I can't tell you what's wrong.

    --Eric
     
  10. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    This is what i was trying.

    Code (CSharp):
    1. fishingLevelText.text = ("" +  GameController.control.currentFishingXP /GameController.control.requieredFishingXP * 100 + "%" ).ToString("F2");
    2.  
    3. i also tried adding it to the end of each of the 2 variables instead and still got the error. i think i am just miss understanding where i am suppose to add it to.
    thanks again for the help
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You have to convert have to convert the numbers only, and then add other stuff like % symbols.

    --Eric
     
  12. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    I usually use :

    Code (CSharp):
    1. Math.Round(number, 2, MidpointRounding.AwayFromZero).ToString("0.00");
    This way it gets rounded to the second decimal but still represented as 2 decimals as a string.
     
  13. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    oh cool thank you, i will see if i can get that way to work. so "number" would be the actual variable I am wanting to have rounded and displayed?
     
  14. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    Thank you very much that worked beautifully!
     
  15. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    I'm glad it worked for you!
     
    Sylvir likes this.