Formating Numbers
pzolla
15 Oct 2012, 18:35Any suggestions on how to format numbers (integers or doubles) displaying a 1,000 separator?
For example, I need to take 54000 and have it display as: 54,000
For example, I need to take 54000 and have it display as: 54,000

jaynabonne
15 Oct 2012, 21:44Here's a little function to do it:
FormatNumber(1234) = "1,234"
FormatNumber(47827824759459) = "47,827,824,759,459"
<function name="FormatNumber" parameters="number" type="string">
numStr = ToString(number)
s = ""
while (LengthOf(numStr) > 3) {
s = "," + Right(numStr, 3) + s
numStr = Left(numStr, LengthOf(numStr)-3)
}
return(numStr + s)
</function>
FormatNumber(1234) = "1,234"
FormatNumber(47827824759459) = "47,827,824,759,459"
pzolla
16 Oct 2012, 02:23Thanks. That worked great.
Can I ask another question? I noticed you used 'while'. I have never used 'while' or, I should say, I did not know about 'while'. I would have normally used 'if' without an 'else'. Can you explain when it is more appropriate to use 'while'?
Can I ask another question? I noticed you used 'while'. I have never used 'while' or, I should say, I did not know about 'while'. I would have normally used 'if' without an 'else'. Can you explain when it is more appropriate to use 'while'?

jaynabonne
16 Oct 2012, 08:24An "if" statement is handy if you want something to be done conditionally - only do this piece of code "if" some condition is true. If the condition isn't true, the code is skipped (unless you have an else, etc), but the flow of code ends up passing through once and then moving on. A real life example would be a door - if the door is locked, then ring the doorbell.
In terms of code flow, it will flow through once.
"while" allows you to have a loop. ("for" and "foreach" do as well). A loop lets you execute some chunk of code over and over until some condition tells you to exit. The real life example would be a door as well - ring the bell. If nobody opens the door, then go back and ring the bell again.
This code will loop over and over until the condition is true. This loop is a bit naive as it will keep ringing the bell forever if nobody answers the door. You'd probably want to have some other condition for exiting in this case.
I hope that makes a little bit of sense. If you haven't used "while" before, try it out. It has its place in your scripting toolbox.
if (DoorIsLocked) {
RingBell()
}
In terms of code flow, it will flow through once.
"while" allows you to have a loop. ("for" and "foreach" do as well). A loop lets you execute some chunk of code over and over until some condition tells you to exit. The real life example would be a door as well - ring the bell. If nobody opens the door, then go back and ring the bell again.
while (DoorIsLocked) {
RingBell
WaitAWhile
}
This code will loop over and over until the condition is true. This loop is a bit naive as it will keep ringing the bell forever if nobody answers the door. You'd probably want to have some other condition for exiting in this case.

I hope that makes a little bit of sense. If you haven't used "while" before, try it out. It has its place in your scripting toolbox.