# How to create currency?

I am super new to unity scripting and I am creating a game. I want to make currency for the game. How could I create a currency system which will let user buy items/earn money

1 Like

var moneyInAccount : float;
var money : float;

function earnedMoney()
{
moneyInAccount = moneyInAccount + money;
}

this is a very simple idea, easily implemented.

2 Likes

Thanks

You can also use the .NET Decimal type, which lets you then easily deal with all of those pesky amounts like dollars and cents, pounds and pennies, and so forth. Renman3000 has given a good starting point and is correct, it is very easy to implement.

This is true. I developed a money system my last project. It worked but i had to manually adjust for cents into dollars, if there is something that can help account for hat, even better!

Good luck!!!

Limiting redundancies C#/JS:

``````moneyInAccount += money;
``````

â€ś+=â€ť goes the same for other operators.

Also - to automatically have a â€śmoneyâ€ť value show up as X.xx - C# allows you to use â€śMâ€ť after assigning a variable (I havenâ€™t done this in Unity tho).

``````decimal moneyInAccount = 0.00M;
decimal money = 0.00M;
``````

The â€śMâ€ť literally stands for â€śMoneyâ€ť haha!

So in js can i use this?

And decimal is a var type ( or only in c#?)

The best way to make a currency system is to keep your money variable in PlayerPrefs XML file. In this way it wonâ€™t be change if you turn off Unity or Stop the scene. To Change the variable value you have to use differenet functions like PlayerPresfs.SetInt .
Check this out :

Is very useful.

Good Luck !

Sheva

Indeed. JS can use +=, -=, etc. Itâ€™s just the shorthand method of using operators for simple math.

x = x + y;

Is the same as:

x += y;

Whatever is on the left is added to whatever is on the right. Multiply, divide, subtract, etc - all work the same. As for â€śdecimalâ€ť and â€śMâ€ť - Iâ€™m not too sure about JS. I just remember using them to create a Windoze app in C# to keep track of my paycheck from workâ€¦ you knowâ€¦ in case they â€śmessed upâ€ť somewhere on my check ha!

Yah I am just wondering, what is the difference between

money = 0.00M
and
money = 0.00f

and I will have to look into it, but you declared the var type as decimal. Never seen that before brah!

For currency I donâ€™t see why it would be so hard to take cents into dollars. You would simply do something as the following.
This is starting with 2 dollars and 420 centsâ€¦

``````public float moneyDollar = 2.0f;
public float moneyCents = 420.0f;

void Update () {
if(moneyCents >= 100) {
moneyCents -= 100;
moneyDollar += 1;
}else{
return;
}
}
``````

that would end with 6 dollars and 20 cents.
Having the floats public would also allow you to add money/take away money through other scripts, like a merchant selling you something.

Also, if you wanted to display it as \$6.20 you could simply use a label like this:

``````	void OnGUI() {
float moneyDollar = 6.0f;
float moneyCents = 20.0f;
GUI.Label(new Rect(10,10,70,50), "Money: \$"+moneyDollar+"."+(moneyCents/10));
}
``````
1 Like

Surprised to see people using floats.

When dealing with money Iâ€™d only ever use Decimal or Int64.

â€śMâ€ť - 2 decimal places.

I donâ€™t believe Iâ€™ve ever used a float - itâ€™s accuracy is only limited to 6 numbers. For conventional fractions I use â€śdoubleâ€ť - since it yields a greater accuracy (15, I believe). For currency I always use â€śdecimalâ€ť for itâ€™s insane accuracy (10 to the -+ 28th) and no rounding problems - however it is much much much slower to use than a standard int.

Personally, for a game currency - I wouldnâ€™t recommend it over â€śdoubleâ€ť - since weâ€™re not dealing with literal money so accuracy and decimal notation might not be needed when you can just truncate. Or for the quick route - an int/100 cast as a double will yield a number with 2 decimal places (provided you have a remainder - again why â€śMâ€ť is used to dictate displaying a value with 2 decimals).

Many ways to tackle â€śgameâ€ť currency but the standard accuracy for any type of currency should be of type decimal. Itâ€™s bigger, slower, but more accurate. All depends on what you want out of your game.

Personally, I find people like whole numbers better than calculating change. Which is why everyone says â€ś5 bucksâ€ť when the quoted price is â€ś4.99â€ť. Humans usually refer to finances as literals and not reals.

Just my 0.02M;

Nice.

M simply represents the decimal type - not money.

The decimal type is just a float [like the single and double] except that it differs in three key areaâ€™s:

1. Internally it contains a decimal number - so numbers like 0.1 can be represented accurately.

2. The scale of the range is relatively small but the precision is remarkably high.

3. Decimal does not have NaN, infinity etc. Small numbers round to zero, large numbers through an exception.

Float, Double, Int, Double all have different ToString formats - so donâ€™t confuse what ToString() returns with the data-structure. For example, M does not have 2 decimal places but about 28. Just a tiny difference

Short demonstration: http://pastebin.com/yW0RNkJv

I gotta re-read that chapter in my C# book - it briefly went over the differences but it sounds like it didnâ€™t fully sink in ha!