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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Hide decimals in float?

Discussion in 'Scripting' started by Bren2468, Jun 8, 2015.

  1. Bren2468

    Bren2468

    Joined:
    Jun 8, 2015
    Posts:
    4
    Hello, in my game I want to be able to make things cost money and use decimal values when they buy things, although I don't want to display the decimals to the player. Is there any way I can do this? Here is my code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class Click : MonoBehaviour {
    6. public UnityEngine.UI.Text moneyDisplay;
    7. public float money = 0f;
    8. public int moneyPerClick = 1;
    9. public float moneyPerSecond = 0;
    10. public string TextName;
    11. public AScript Function;
    12. public UnityEngine.UI.Text moneyPerClickDisplay;
    13. string finalMoney;
    14. public UnityEngine.UI.Text moneyPerSecondDisplay;
    15.  
    16.    void FixedUpdate()
    17.   {
    18.   finalMoney = AScript.Function(money, false, false);
    19.   moneyDisplay.text = TextName + finalMoney; //I want to get an int version of the finalMoney string to display. The function takes the money float and checks for stuff and returns a string. It has to be returned as a string or it doesn't work.
    20.   moneyPerClickDisplay.text = "MPC:$" + moneyPerClick;
    21.   moneyPerSecondDisplay.text = "MPS:$" + moneyPerSecond;
    22.    }
    23.  
    24.    public void Clicked()
    25.   {
    26.      money += moneyPerClick;
    27.    }
    28. }
    So yeah. Is there any way to do this? Thanks in advance!
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    String.format?
     
    SparrowGS likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Using custom numeric format strings you can use either string.Format (as Korno suggested), or just the ToString method of float.
    https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx

    Code (csharp):
    1.  
    2. moneyPerClickDisplay.text = "MPC:$" + moneyPerClick.ToString("0");
    3.  
    Note that the 0 symbol says to show the digit if one is present, otherwise show 0. The thing is the format string will always show everything left of the format string, as they are more significant. But won't show anything less significant (right of) than that defined in the format string.
     
    abadazouhir likes this.
  4. RabenFutter

    RabenFutter

    Joined:
    Dec 30, 2012
    Posts:
    38
    a alternative would be to just call it an int with:

    Code (CSharp):
    1. moneyPerClickDisplay.text = "MPC:$" + ((int)moneyPerClick).ToString();
     
    baskan379 and StefyLord like this.
  5. JakeLeB

    JakeLeB

    Joined:
    Apr 18, 2013
    Posts:
    25
    If you use .ToString(), inside the () you can use quotes to surround a number to choose how many numbers follow the float. E.g. ToString("f2") would give 11.19 but ToString("f0") would give 11.
     
    gwdyce, d2tz, AppCity and 1 other person like this.
  6. Bren2468

    Bren2468

    Joined:
    Jun 8, 2015
    Posts:
    4
    I want to get an int value of totalMoney which is a string, not a float. Thanks for all the ideas though!
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Sounds like you may want to look at int.Parse() and int.TryParse()
     
  8. JakeLeB

    JakeLeB

    Joined:
    Apr 18, 2013
    Posts:
    25
    In that case do (int)string, just make sure its all numbers, no text or float values.
     
  9. abadazouhir

    abadazouhir

    Joined:
    Oct 21, 2018
    Posts:
    4
    THANK U SO MUCH :)