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.

1 Like

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.

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

@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