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

looking for data type

Discussion in 'Scripting' started by QashiCheema, Jun 12, 2018.

  1. QashiCheema

    QashiCheema

    Joined:
    May 18, 2015
    Posts:
    5
    hey . i am new to C#, and after a long time came towards coding, can anyone tel me how can i show in canvas 0.456789 to 0.45 only floating type value .
     
  2. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    1) By showing in Canvas i hope you mean showing on UI. You can check Unity Tutorial, there you'll find how to work with Ui Text and floats.

    Tutorial link:
    https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial/displaying-score-and-text

    2) You can convert 0.456789 to 0.45 by using Math.Truncate.

    Code (CSharp):
    1. float number = 0.456789f;
    2. float roundedNumber = (float)System.Math.Truncate(number * 100) / 100; // will return 0.45
    It will not round your float, if you want to round use this:

    Code (CSharp):
    1. float number = 0.456789f;
    2. float roundedNumber = (float)System.Math.Round(number, 2); // will return 0.46