Search Unity

Rounding down numbers with TextMeshPro

Discussion in 'Scripting' started by serbusfish, Oct 19, 2018.

  1. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    In my game I display a score multiplier during gameplay, when I set this up I used the standard GUIText and through a simple piece of code made it always round down the number, so instead of it displaying 'x 1.899' for example it will always use whole numbers so it would display as 'x 1.90'. To achieve this I did:

    Code (csharp):
    1.  
    2. multiText.text = "Multiplier: x " + multiValue.ToString("f2");
    3.  
    I switched over to using TextMeshPro and now this code doesn't work, i'm back to seeing the unrounded numbers in game. Can anyone tell me what I need to do to make this work with TextMeshPro?
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    I'm surprised that doesn't work, your use of TextMesh should not affect the output of ToString("f2").

    Try this:
    Code (CSharp):
    1. multiText.text = "Multiplier: x " + System.Math.Round(multiValue, 2).ToString("f2");
     
  3. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    EDIT:

    Thanks pal but this hasn't fixed the problem unfortunately, i'm still getting instances of numbers such as 1.69999, etc
     
    Last edited: Oct 19, 2018
  4. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Can you post the method that updates the text of the component? The code that you posted wshould work just fine, I'm definitely with @Gambit-MSplitz here.

    Perhaps it's being updated from a different script or something. You can add Debugs and/or break-points to figure out
    1. whether that code is being executed
    2. what the result of the ToString(...) is
    3. what the concatenated string is
    and so on.