Author Topic: Adding commas to numbers  (Read 1778 times)

Windlord

  • Guest
Adding commas to numbers
« on: January 11, 2011, 03:10:35 pm »
Code: (squirrel) [Select]
// This function adds commas every 3 digits for outputs like 12,345,678
function ToThousands ( num )
{
num = num.tostring();
local last = num.find(".") - 1;
local chk = last % 3;
local decimaltrig = false;
local output = "";

foreach ( idx, digit in num )
{
output += digit.tochar();
if ( digit == 44 ) decimaltrig = true;
if ( !decimaltrig && idx < last && idx % 3 == chk ) output += ",";
}
return output;
}

This returns numbers in the following format:
1,000,000
1,234
542
189,23

Floats now work.
« Last Edit: April 25, 2011, 04:38:45 am by Windlord »

Windlord

  • Guest
Re: Adding commas to numbers
« Reply #1 on: April 25, 2011, 04:38:25 am »
Fixed previous version to allow for decimals!

 

© Liberty Unleashed Team.