Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Peysbubby · Aug 21, 2015 at 07:03 PM · c#currency

How to get my currency to show up as dollars/cents?

I have my game set so that when you earn a penny it increases a float by o.o1f, when you collect nickel it increases by 0.05f and when you collect a dime it increases by 0.1f. However the money text ends up showing this somehow: alt text

I cant seem to figure out why if i'm increasing it by .01, .05, or .1 how I can end up with .0000.

is there a better way to create currency, or am I just simply doing something else wrong?

This is my script it is in c#.

   void OnTriggerEnter(Collider other)
     {
         if (other.tag == "penny") 
         {
             Destroy (other.gameObject);
             GameController.Money = GameController.Money + GameController.pennyWorth;
             GameController.LME = GameController.LME + GameController.pennyWorth;
             GameController.penniesInGame =  GameController.penniesInGame - 1;
             GameController.coinsInGame = GameController.coinsInGame - 1;
             GameController.METS = GameController.METS + GameController.pennyWorth;
         }
 
         if (other.tag == "nickel") 
         {
             Destroy (other.gameObject);
             GameController.Money = GameController.Money + GameController.nickelWorth;
             GameController.LME = GameController.LME + GameController.nickelWorth;
             GameController.nickelsInGame =  GameController.nickelsInGame - 1;
             GameController.coinsInGame = GameController.coinsInGame - 1;
             GameController.METS = GameController.METS + GameController.nickelWorth;
         }
 
         if (other.tag == "dime") 
         {
             Destroy (other.gameObject);
             GameController.Money = GameController.Money + GameController.dimeWorth;
             GameController.LME = GameController.LME + GameController.dimeWorth;
             GameController.dimesInGame =  GameController.dimesInGame - 1;
             GameController.coinsInGame = GameController.coinsInGame - 1;
             GameController.METS = GameController.METS + GameController.dimeWorth;
         }
     }
untitled432.png (2.5 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by cjdev · Aug 21, 2015 at 07:14 PM

You are likely running into floating point imprecision here. Your best bet would probably be to convert your floats into ints and represent your currency as an amount of cents rather than dollars, converting it to a float right before displaying it.

Comment
Add comment · Show 2 · 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 Peysbubby · Aug 21, 2015 at 07:31 PM 0
Share

@cjdev I am not sure if this is exactly what you meant but it seams to be working. I made two new variables, one called cents and one called dollars. I increase the cents variable directly and use

if (Cents >= 100) { Dollars = Dollars + 1; Cents = 0; }

to increase the dollars variable.

avatar image cjdev · Aug 21, 2015 at 08:16 PM 0
Share

You could do it that way if you need to, but you could also just keep the money stored in cents and convert it to dollars by dividing by 100 when you assign it to your money text.

avatar image
0

Answer by brian-nielsen · May 09 at 08:32 PM

Not exactly the answer but this page helped me in my currency game. Could prove useful to someone https://gamedevplanet.com/number-formatting-in-unity-useful-c-tostring-formats/

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

28 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Currency System Not Saving!? 1 Answer

Flip over an object (smooth transition) 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