Does anyone know a good code for using Money format for inside Unity? I’ve done a search online but I couldn’t get any of the codes I found to work. Basically I would my text field to populate in this format $9,999,999,999. Without cents and up to 10 billion dollars.
It seems like it would involve a lot more code then I would have initially thought it would require. I think it’s going to need quite a bit of string manipulation, and variable converting. I can give you an idea in pseudocode, I’m not that knowledgeable in javascript or c# yet, this is probably fairly close to give you an idea…
var moneyNumber as int;
var moneyString as string = moneyNumber.ToString;
//only a hypothetical test for over 1000 for comma placement
if (moneyNumber > 999 And < 10000) {
moneyString = "$" moneyString.Substring(0,1) "," moneyString.Substring(1, (moneyString.Length - 1))
}
Other checks would probably be needed to check whether or not certain commas need to be added.
Hope this helps!
MetaPsychosis,
Thanks for replying. I’ve tried added your code and the only problem is… Im not that knowledgeable in Javascript also, so I’ve run into formatting issues that Im not sure what to do to fix it. I fixed the variable declaration:
var moneyNumber : int;
var moneyString : string = moneyNumber.ToString;
But the rest of the code is giving me errors that Im not sure what to do to fix them… Hmm, I guess I’ll keep researching. If you come up with something please let me know! Thanks!!
You can pass a format string to “ToString” so you can customize as you need, take a look at Standard Numeric Format Strings
pseudo-code is not real code, it’s used as a non-language, non-syntax specific subsitute to explain an idea for code. so that’s why it didn’t work
Not really an answer to your question, but: Depending on your needs I would advice you to store your money as cents, and never use float/double due to their inherit imprecision.
ZioRed,
Thanks for the link. Unfortunately I don’t know C# or VB so I couldn’t apply any of the example codes to my scene. It was a little confusing for me.
I played around with Metaphyschos, code. But Im getting an error. Does anyone know how I can format this properly? Im using for the word “and” in the IF statement, but its giving me the error. Here is the code Im using:
var moneyNumber : int = 60000000;
var moneyString : string;
//Get Component Variables
var textMoney : GUIText;
function Update () {
//Creates the current budget.
if (moneyNumber >= 999 <= 10000) {
moneyString = "$" + moneyString.Substring(0,1) + "," + moneyString.Substring(1, (moneyString.Length - 1))
}
// Displays the current budget text.
textMoney.text = "$ " + moneyNumber;
}
This is the error message:
Assets/Script_SceneManager.js(33,32): BCE0044: expecting ), found ‘’.
Any suggestions?
var value = 12345.6789;
Debug.Log(value.ToString("C0"));
Result:
$12,346
I hate to be the bearer of bad news, but you’re going to have to learn about string formatting for integers. Look it up on MSDN, it’s not at all hard.
Magic ![]()
If the OP was serious about ‘…up to 10 billion dollars…’ then you’d quite simply add an if check:
[untested]
function GetMoneyString(money : Int64) : string
{
if (money < 10000000000)
return money.ToString("C0");
else
return "Computer says no!";
}
JohnnyA, Thank you very much! This works perfectly.
NPSF3000, yes I was serious about the 10 billion and thanks for showing me the IF format to make it work. Appreciate it guys!
JRavey, I have a lot to learn actually. Unfortunately Im new to programming in Javascript but Im learning a lot.
Then ensure that you store the money in Int64 or Decimal. Float and Int will have issues storing such large numbers [assuming you want accuracy of $1].
Thanks for the tip! But Im getting an error message when using Int64. Is my structure correct?
function Update () {
// Start Money Variable.
var money = 999999998;
}
function GetMoneyString(money : Int64) : string {
// Displays the current budget text.
textMoney.text = (money.ToString("C0"));
}
The error Im getting is:
Assets/Script_SceneManager.js(37,33): BCE0018: The name ‘Int64’ does not denote a valid type (‘not found’). Did you mean ‘System.Int32’?
Try using ‘long’. If that fails try ‘System.Int64’.
System.Int64 got rid of the message, Im assuming its accepting it now. But the code doesn’t like ‘string’ on my function line. It gives me this error:
Assets/Script_SceneManager.js(37,49): BCE0018: The name ‘string’ does not denote a valid type (‘not found’). Did you mean ‘Boo.Lang.Compiler.IO.StringInput’?
function GetMoneyString(money : System.Int64) : string {
function GetMoneyString(money : System.Int64) : String {
string must be String (capital S)