- Home /
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:
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;
}
}
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.
@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.
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.
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/
Your answer
![](https://koobas.hobune.stream/wayback/20220612000923im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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