Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by unity_0sdsWZad6Fwp-A · Jan 31, 2019 at 05:45 AM · mathdecimalshortdivision

Truncate number to first decimal place?

Hello, I was wondering how to truncate a number to the tenths place because the number gets too long and I just want it to be shorter but not just a whole number. Also I do not want the numbers to be rounded up or down because I do not want it to affect anything during the gameplay.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 
 
 public class GlobalOres : MonoBehaviour
 {
 
 
     public static float OreCount;
     public GameObject OreDisplay;
     public float InternalOre;
     public float InternalOreDisplay;
     
 
     void Update()
     {
         InternalOre = OreCount;
         InternalOreDisplay = InternalOre;
         OreDisplay.GetComponent<Text>().text = "Ores: " + InternalOreDisplay;
 
         if (OreCount >= 1000 & OreCount <= 1000000)
         {
             InternalOreDisplay /= 1000;
             OreDisplay.GetComponent<Text>().text = "Ores: " + InternalOreDisplay + "K";
         }
         if (OreCount >= 1000000 & OreCount < 1000000000)
         {
             InternalOreDisplay /= 1000000;
             OreDisplay.GetComponent<Text>().text = "Ores: " + InternalOreDisplay + "M";
         }
         if (OreCount >= 1000000000 & OreCount < 10000000000000)
         {
             InternalOreDisplay /= 1000000000;
             OreDisplay.GetComponent<Text>().text = "Ores: " + InternalOreDisplay + "B";
         }
         if (OreCount >= 10000000000000 & OreCount < 100000000000000000)
         {
             InternalOreDisplay /= 10000000000000;
             OreDisplay.GetComponent<Text>().text = "Ores: " + InternalOreDisplay + "T";
 
            
         }
     }
        
     
 }

I tried adding Truncate into the division but it wouldn't divide the numbers, so I scrapped it.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Jan 31, 2019 at 03:26 AM

Well you could use one of the standard string formatting strings. In your case the most suitable would be "F1". Though note that it uses the usual rounding. So 1234.56 would become "1234.6" and 1234.54 would become "1234.5".


If you want to truncate the number, just use Mathf.Floor(x*10)/10 where x is the number you want to truncate to one digit behind the decimal point. I would still recommend to use "F1" since dividing a number by 10 could result in some .99999 number which would be rounded up. I don't have an actual example but i mean something like this: If you have a number like 1234.4896 you multiply it by 10 to get something like 12344.896. Then floor will truncate the fractional part. However due to floating point accuracy the number you may get might be something like 12343.999997 or 12344.000002. Using ToString("F1") will ensure the resulting string will be "1234.4" in each case.


Note that you have to be careful when using &. This is the bitwise "and" operator and not the logical and operator. In almost all cases you want to use && inside if statements to combine several boolean statements. Also your code could be simplified / optimised a lot. First of all it's bad practise to store variable as class members when they are only used inside a method. Use local variables. This simplifies debugging and makes the code more clear. It also does not clutter the class namespace. Next is (apart from using && which would slightly increase performance) you want to use an "else if" chain in such cases since only one of those cases should be valid at a given time.


The whole process of determining the order of magnitude could be done with a simple loop and a static array of strings

 private static string[] suffixes = new string[] {"", "K", "M", "B", "T", "Q"};
 private Text oreText;

 void Start()
 {
     oreText = OreDisplay.GetComponent<Text>();
 }
 void Update()
 {
     float val = OreCount;
     int suffix = 0;
     while (val >= 1000)
     {
         val /= 1000;
         suffix++;
     }

     if (suffix < suffixes.Length)
     {
         val = Mathf.Floor(val*10f) / 10f;
         oreText.text = "Ores: " + val.ToString("F1") + suffixes[suffix];
     }
     else
     {
         // Too large, just use scientific format (i.e. 1.25E+010)
         oreText.text = "Ores: "+ OreCount.ToString("E2");
     }
 }



Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_0sdsWZad6Fwp-A · Jan 31, 2019 at 03:42 AM 0
Share

This was very helpful. Also, thank you for the extra tips!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with strings and decimals 1 Answer

Simple math always returns 0 1 Answer

Why does this alway return 0 1 Answer

How to get floats value without including exponential notation 1 Answer

how to create an inverse-TRS matrix directly? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges