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 /
  • Help Room /
avatar image
2
Question by carlqwe · Apr 11, 2016 at 05:01 PM · decimal

show float in whole numbers not decimal

Hello having a problem, when i add "money" then the GUI will be covered with decimal numbers. For some reason when the float gets decreased or added to it shows in a lot of decimals. How do i fix that?

Also im accessing the float from an other script.

 #pragma strict
 
 var buildControllerScript : BuildController;
 
 function Start () 
 {
     buildControllerScript = GameObject.Find("Main Camera").GetComponent(BuildController);
 }
 
 function Update () 
 {
     buildControllerScript.money += 1 * Time.deltaTime;
 }


The Other script has a lot of other code but in this case for information it only ahs the money float. And no i can't move the float over to this current script.

Comment
Add comment · Show 1
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 aida12_99 · Apr 11, 2016 at 07:09 PM 0
Share

Not sure. But, I think you should replace the code within the Update with the code below:

buildControllerScript.money += 1.0f * (float) Time.deltaTime;

3 Replies

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

Answer by Eno-Khaon · Apr 11, 2016 at 05:21 PM

Fortunately, this can be solved rather simply using Standard Numeric Format Strings. Rather than changing the properties of the float value, change the way that it's displayed:

 string displayedMoney = buildControllerScript.money.ToString("F0");

This example of using "F0" will display the value with 0 decimal places. If you wish to show any decimal places, such as dollars and cents, you can simply use a value like "F2" to display two decimal places.

Alternatively, if you're showing real currency, the options of "C" and "C#" can convert the value into a local or specified regional currency's presentation.

Comment
Add comment · Show 6 · 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 carlqwe · Apr 11, 2016 at 05:42 PM 0
Share

i did as u said but still no progress

 #pragma strict
 
 var buildControllerScript : BuildController;
 var displayed$$anonymous$$oney : String;
 
 function Start () 
 {
     buildControllerScript = GameObject.Find("$$anonymous$$ain Camera").GetComponent(BuildController);
     displayed$$anonymous$$oney = buildControllerScript.money.ToString("F0");
 }
 
 function Update () 
 {
     buildControllerScript.money += 1 * Time.deltaTime;
 }
avatar image Eno-Khaon carlqwe · Apr 11, 2016 at 06:04 PM 0
Share

Well, once you have the string variable, that's what you display in the GUI.

Additionally, it needs to be updated any time the "money" variable is updated, so it should be updated every frame as well:

 function Update()
 {
     buildControllerScript.money += 1 * Time.deltaTime;
     displayed$$anonymous$$oney = buildControllerScript.money.ToString("F0");
 }

That said, the conversion would best be applied in whichever script you're displaying the value in the GUI (in case it's not the one you've given examples from).

avatar image carlqwe Eno-Khaon · Apr 11, 2016 at 06:12 PM 0
Share

Thanks for you help but still i dont understand,

 var displayed$$anonymous$$oney : String;
 
 function Update ()
 {        
     //Other
     displayed$$anonymous$$oney = money.ToString("F0");
 }

Show more comments
avatar image OreoSplitter · Jun 09, 2018 at 04:05 PM 0
Share

Thanks a lot! I was using .ToString("0") to get rid of decimals but .ToString("F0"), no garbage.

avatar image
2

Answer by b1gry4n · Apr 11, 2016 at 07:26 PM

There are 100 pennies in a dollar. Since we are adding/subtracting coins we need to convert our current dollar amount to pennies (the smallest form) and do the math there. After all, $1.52 is really just 152 pennies. Here is an example:

         float addThis = amount * 100;
         float toThis = currentCash * 100;
         float result = addThis + toThis;
         currentCash = result / 100; 

If you want to display ONLY the dollar amount and dont really care about the accuracy of your cash (since Mathf.Round will always round up)

 wholeAmount = Mathf.Round(currentCash);

You are using Time.deltaTime to add to your cash pool. This will result in numbers that = 1.4363543 since its deltaTime! I suggest adding to your cash like this (it seems like this is what you wanted to do anyways). I may have gone too in depth for your question!

 currentCash += 1;

Lastly, if you are intent on using Time.deltaTime for calculating your currency, but you want it to display whole numbers only, you can convert your float to an int.

         deltaTimeCash += 1 * Time.deltaTime;
         cashInt = (int)deltaTimeCash;

You can also use Time.deltaTime to add cash on a per coin basis and convert it to a readable dollar amount

         deltaTimeCoins += Time.deltaTime;
         if(deltaTimeCoins > 1.0f) {
             deltaTimeCoins = 0.0f;
             coins += 1;
         }
         if(coins == 100) {
             coins = 0;
             cash += 1;
         }
         CashAndCoins(coins, cash);
 
     void CashAndCoins(int tCoin, int tCash) {
         float tc = tCash * 100;
         float unres = tc + tCoin;
         cashAndCoins = unres / 100;
     }

SO, all together now...

     public float currentCash = 0.0f;
     public float currentRoundedCash = 0.0f;
     public float deltaTimeCash;
     public int cashInt = 0;
     public float deltaTimeCoins;
     public int cash;
     public int coins;
     public float cashAndCoins;
     void Update() {
         //
         if (Input.GetKeyDown(KeyCode.O))
         {
             MoneyChange(5.23f); // add
         }
         if (Input.GetKeyDown(KeyCode.P))
         {
             MoneyChange(-7.55f); // subtract
         }
         //
         currentCash += 1;
         //
         currentRoundedCash = Mathf.Round(currentCash);
         //
         deltaTimeCash += 1 * Time.deltaTime;
         cashInt = (int)deltaTimeCash;
         //
         deltaTimeCoins += Time.deltaTime;
         if(deltaTimeCoins > 1.0f) {
             deltaTimeCoins = 0.0f;
             coins += 1;
         }
         if(coins == 100) {
             coins = 0;
             cash += 1;
         }
 
         CashAndCoins(coins, cash);
     }
 
     void MoneyChange(float amount)
     {
         float addThis = amount * 100;
         float toThis = currentCash * 100;
         float result = addThis + toThis;
         currentCash = result / 100;        
     }
 
     void CashAndCoins(int tCoin, int tCash) {
         float tc = tCash * 100;
         float unres = tc + tCoin;
         cashAndCoins = unres / 100;
     }

There has to be SOMETHING in this that you can use :)

Comment
Add comment · 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
2

Answer by Redwolve · Apr 11, 2016 at 09:51 PM

@carlqwe

There are a few different ways to do this and it can all depend on how you want to display the data and also how you'd like to store it for use later.

To go along with what @Eno Khaon said, you can do the String thing. However I have noticed that for myself it can get you in trouble sometimes. Now you will be displaying a version of your number that isn't necessarily what you have stored.

Same can go for the way @b1gry4n did it. Using Round() can get you in wierd situations because now you are displaying a number potentially larger than what you have stored.

Here is what I thought up initially, and should work for you depending on how accurate the money needs to be and how random the deltaTime pulls are. Its is very similar to b1gry4n but instead of using the Round() you use Floor().

So take your Money and multiply it by 100, and this doesn't have to only be used with money numbers you base the multiplier on the decimals you want. if you only want 1 decimal space you multiply by 10. Then take your number and apply the Mathf.Floor() function and this will round down to the nearest int. ( I find most times i would rather use Mathf.Floor() and .Ceil() more often than round, but hey thats all based on the uses. ) and then just multiply by .01 ( I choose multiply only because it takes less in programming to multiply than it does to divide, you can also choose to just divide by 100)

This would make you numbers look like so:

Money Prior to : $32.23

deltaTime : 1.23434534

deltaTime X 100 : 123.434534

deltaTime W Ceil : 123

deltaTime X .01 : 1.23

Money + deltaTime : $33.46

Comment
Add comment · 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

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

47 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

Related Questions

Make a float display 10:07 instead of 10.7 2 Answers

How to identify 2 decimal places? 2 Answers

Changing prices after a certain point? 2 Answers

Problem with using decimals as primary number 0 Answers

UI Text to Decimal. How? 3 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