function AddCommas takes integer whichInteger returns string
local string s = ""
local integer sign = 1
local integer i = 0
local integer j = 0
local integer k = 0
// Store the sign and if negative, make whichInteger positive so we don't have to deal with that nasty negative dividend stuff
if (whichInteger < 0) then
set sign = -1
set whichInteger = -whichInteger
endif
// Check if there needs to be commas
if (whichInteger < 1000) then
return I2S(whichInteger * sign) // No commas to add
endif
// Add the commas using nifty modulo and some maths
loop
exitwhen whichInteger < 1000
// Find the exponent as a truncated integer for some place counting
set i = R2I(Log(I2R(whichInteger)))
// Exponentiate 10 to the exponent we found earlier
set k = R2I(Pow(10, i))
// Use k to get the leading digit
set j = whichInteger / k
set s = s + I2S(j)
// If the exponent is divisible by 3 then it warrants a comma
if ( i - (i / 3) * 3 == 0 ) then
set s = s + ","
endif
// Subtract the leading digit
set whichInteger = whichInteger - (j * k)
endloop
// Add the remaining three digits
set s = s + I2S(whichInteger)
// Add the negative sign if the original was negative
if (sign == -1) then
set s = "-" + s
endif
return s
endfunction
//==============================================================================
// Logarithm script by X - Version 1.0 - December 12, 2009
//==============================================================================
//
// about:
// - compute natural logarithm of a real.
// - compute logarithm to the base b of a real.
//
// Usage:
// - Ln(x) to compute the natural logarithm of the real x
// - LogB(x, b) to compute the logarithm to the base b of the real x
// - LogBV2(x, b) to compute the logarithm to the base b of the real x with another method, see Notes
// - Logarithm_e to acces the value of the constant e
//
// Notes:
// - Ln(x) is exactly the same as LogB(x,Logarithm_e)
// - LogBV2(x,b) is Ln(x)/Ln(b) , make your own tests to compare it to LogB(x,b)
//
// - you can find the algorithm used at this address :
// [url]http://en.literateprograms.org/Logarithm_Function_%28Python%29[/url]
// it is the same as the basic one explained by Donald E. Knuth in his book
//
// - reals in jass are not very precise, something like 9 total digits. Be happy if it works
//
// Requirements:
// - JassHelper.
//
// Installation:
// - Create a new trigger called Logarithm.
// - Convert it to custom text and replace all the code with this code.
//
// Special Thanks:
// - Ammorth: for is LinkedList information layout
//
//==============================================================================
library Logarithm
globals
public constant real e = 2.718282 //e=2.71828182845904523536
private constant real EPSILON = 0.0000001 // dont use lower values, jass reals sucks
endglobals
function Ln takes real x returns real
local real decimal = 0.0
local real partial = 0.5
local integer i = 0
if x <= 0 then
return 0
endif
loop
exitwhen x >= 1
set i = i - 1
set x = x * e
endloop
loop
exitwhen x < e
set i = i + 1
set x = x / e
endloop
set x = Pow(x,2) // maybe power is more accurate than x*x -_-'
loop
exitwhen partial <= EPSILON
if x >= e then
set decimal = decimal + partial
set x = x / e
endif
set partial = partial * 0.5
set x = Pow(x,2)
endloop
return i + decimal
endfunction
function LogBV2 takes real x, real base returns real
return Ln(x)/Ln(base)
endfunction
function LogB takes real x, real base returns real
local real decimal = 0.0
local real partial = 0.5
local integer i = 0
if x <= 0 or base <=0 then
return 0
endif
loop
exitwhen x >= 1
set i = i - 1
set x = x * base
endloop
loop
exitwhen x < base
set i = i + 1
set x = x / base
endloop
set x = Pow(x,2)
loop
exitwhen partial <= EPSILON
if x >= base then
set decimal = decimal + partial
set x = x / base
endif
set partial = partial * 0.5
set x = Pow(x,2)
endloop
return i + decimal
endfunction
function Log takes real x returns real
return LogB(x, 10)
endfunction
endlibrary
//==============================================================================
// End of Logarithm script
//==============================================================================
function AddCommas takes integer whichInteger returns string
local integer sign = 1
local string s = ""
local string t = ""
local integer i = 0
local boolean b = false
// Store the sign and if negative, make whichInteger positive so we don't have to deal with that nasty negative dividend stuff
if (whichInteger < 0) then
set sign = -1
set whichInteger = -whichInteger
endif
// Check if there needs to be commas
if (whichInteger < 1000) then
return I2S(whichInteger * sign) // No commas to add
endif
// Convert to string and iterate
set t = I2S(whichInteger)
loop
// We are working backwards so it may be a bit confusing
set i = StringLength(t)
// As long as the number of digits is 3 or more, we can work with it
if (i > 2) then
// Set it to the last three digits in t and concatenate it with s
set s = SubString(t, i - 3, i) + s
// Remove those three digits from t
set t = SubString(t, 0, i - 3)
else
// It has less than three digits, so we can just slap the rest into s
set s = t + s
set b = true
endif
if (b == false) then
// We've still got more digits, so we need a comma
set s = "," + s
else
// No more digits, exit the loop
exitwhen true
endif
endloop
// Add the negative sign if the original was negative
if (sign == -1) then
set s = "-" + s
endif
return s
endfunction
function AddCommas takes integer whichInteger returns string
local integer sign = 1
local string s = ""
local string t = ""
local integer i = 0
local boolean b = false
// Store the sign and if negative, make whichInteger positive so we don't have to deal with that nasty negative dividend stuff
if (whichInteger < 0) then
set sign = -1
set whichInteger = -whichInteger
endif
// Check if there needs to be commas
if (whichInteger < 1000) then
return I2S(whichInteger * sign) // No commas to add
endif
// Convert to string and iterate
set t = I2S(whichInteger)
loop
// We are working backwards so it may be a bit confusing
set i = StringLength(t)
// As long as the number of digits is 3 or more, we can work with it
if (i > 2) then
// Set it to the last three digits in t and concatenate it with s
set s = SubString(t, i - 3, i) + s
// Remove those three digits from t
set t = SubString(t, 0, i - 3)
else
// It has less than three digits, so we can just slap the rest into s
set s = t + s
set b = true
endif
if (b == false) then
// We've still got more digits, so we need a comma
set s = "," + s
else
// No more digits, exit the loop
exitwhen true
endif
endloop
// Add the negative sign if the original was negative
if (sign == -1) then
set s = "-" + s
endif
return s
endfunction
//==============================================================================
// Logarithm script by X - Version 1.0 - December 12, 2009
//==============================================================================
//
// about:
// - compute natural logarithm of a real.
// - compute logarithm to the base b of a real.
//
// Usage:
// - Ln(x) to compute the natural logarithm of the real x
// - LogB(x, b) to compute the logarithm to the base b of the real x
// - LogBV2(x, b) to compute the logarithm to the base b of the real x with another method, see Notes
// - Logarithm_e to acces the value of the constant e
//
// Notes:
// - Ln(x) is exactly the same as LogB(x,Logarithm_e)
// - LogBV2(x,b) is Ln(x)/Ln(b) , make your own tests to compare it to LogB(x,b)
//
// - you can find the algorithm used at this address :
// [url]http://en.literateprograms.org/Logarithm_Function_%28Python%29[/url]
// it is the same as the basic one explained by Donald E. Knuth in his book
//
// - reals in jass are not very precise, something like 9 total digits. Be happy if it works
//
// Requirements:
// - JassHelper.
//
// Installation:
// - Create a new trigger called Logarithm.
// - Convert it to custom text and replace all the code with this code.
//
// Special Thanks:
// - Ammorth: for is LinkedList information layout
//
//==============================================================================
library Logarithm
globals
public constant real e = 2.718282 //e=2.71828182845904523536
private constant real EPSILON = 0.0000001 // dont use lower values, jass reals sucks
endglobals
function Ln takes real x returns real
local real decimal = 0.0
local real partial = 0.5
local integer i = 0
if x <= 0 then
return 0
endif
loop
exitwhen x >= 1
set i = i - 1
set x = x * e
endloop
loop
exitwhen x < e
set i = i + 1
set x = x / e
endloop
set x = Pow(x,2) // maybe power is more accurate than x*x -_-'
loop
exitwhen partial <= EPSILON
if x >= e then
set decimal = decimal + partial
set x = x / e
endif
set partial = partial * 0.5
set x = Pow(x,2)
endloop
return i + decimal
endfunction
function LogBV2 takes real x, real base returns real
return Ln(x)/Ln(base)
endfunction
function LogB takes real x, real base returns real
local real decimal = 0.0
local real partial = 0.5
local integer i = 0
if x <= 0 or base <=0 then
return 0
endif
loop
exitwhen x >= 1
set i = i - 1
set x = x * base
endloop
loop
exitwhen x < base
set i = i + 1
set x = x / base
endloop
set x = Pow(x,2)
loop
exitwhen partial <= EPSILON
if x >= base then
set decimal = decimal + partial
set x = x / base
endif
set partial = partial * 0.5
set x = Pow(x,2)
endloop
return i + decimal
endfunction
function Log takes real x returns real
return LogB(x, 10)
endfunction
endlibrary
//==============================================================================
// End of Logarithm script
//==============================================================================
// TEST CODE PORTION
scope myScope initializer init
private function init takes nothing returns nothing
call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, AddCommas(2147483647))
endfunction
endscope
library AddCommas
function AddCommas takes integer n returns string
local integer m = 0
local string s = ""
loop
exitwhen n == 0
if (m > 100) then
set s = "," + s
endif
set m = n
set n = n/1000
set m = m-n*1000
set s = I2S(m) + s
endloop
if (s == "") then
return "0"
endif
return s
endfunction
endlibrary
library AddCommas
function AddCommas takes integer n returns string
local integer m = 0
local string s = ""
local boolean b = false
if (n < 0) then
set n = -n
set b = true
endif
loop
exitwhen n == 0
if (m > 0) then
set s = "," + s
endif
set m = n
set n = n/1000
set m = m-n*1000
if (n > 0) then
if (m < 10) then
set s = "00" + I2S(m) + s
elseif (m < 100) then
set s = "0" + I2S(m) + s
else
set s = I2S(m) + s
endif
set m = 1
else
set s = I2S(m) + s
set m = 0
endif
endloop
if (s == "") then
return "0"
endif
if (b) then
set s = "-" + s
endif
return s
endfunction
endlibrary
struct tester extends array
private static method onInit takes nothing returns nothing
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, AddCommas(-30000000))
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, AddCommas(-3000000))
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, AddCommas(-300000))
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, AddCommas(-30000))
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, AddCommas(-3000))
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, AddCommas(-300))
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, AddCommas(-30))
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, AddCommas(-3))
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, AddCommas(0))
endmethod
endstruct
Also nestharus is right about your code being wrong. Right now mine is the only script here hat works 100% of the time. Albeit it's slow but it works.
I forgot wc doesn't work with large math functions as fast as it can manipulate strings much like python
function Trig_Comma_Func001Func003C takes nothing returns boolean
if ( not ( udg_int > 999 ) ) then
return false
endif
return true
endfunction
function Trig_Comma_Func001C takes nothing returns boolean
if ( not ( udg_int > 999 ) ) then
return false
endif
return true
endfunction
function Trig_Comma_Actions takes nothing returns nothing
if ( Trig_Comma_Func001C() ) then
set udg_str2 = SubStringBJ(udg_str, ( StringLength(udg_str) - 2 ), StringLength(udg_str))
set udg_int = ( udg_int / 1000 )
if ( Trig_Comma_Func001Func003C() ) then
set udg_str = SubStringBJ(udg_str, ( StringLength(udg_str) - 5 ), ( StringLength(udg_str) - 3 ))
set udg_int = ( udg_int / 1000 )
set udg_str = ( I2S(udg_int) + ( "," + ( udg_str + ( "," + udg_str2 ) ) ) )
else
set udg_str = ( I2S(udg_int) + ( "," + udg_str2 ) )
endif
else
endif
endfunction
Is their any script that removes those char arrays that stay in mem too or no..
New Comma sys
Events
Player - Player 1 (Red) types a chat message containing -num as A substring
Conditions
Actions
Set Temp_Integer = (Integer((Substring((Entered chat string), 6, (Length of (Entered chat string))))))
Set Temp_String = (String(Temp_Integer))
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
If - Conditions
Temp_Integer Greater than 999
Then - Actions
Set Temp_String2 = (Substring(Temp_String, ((Length of Temp_String) - 2), (Length of Temp_String)))
Set Temp_Integer = (Temp_Integer / 1000)
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
If - Conditions
Temp_Integer Greater than 999
Then - Actions
Set Temp_String = (Substring(Temp_String, ((Length of Temp_String) - 5), ((Length of Temp_String) - 3)))
Set Temp_Integer = (Temp_Integer / 1000)
Set Temp_String = ((String(Temp_Integer)) + (, + (Temp_String + (, + Temp_String2))))
Else - Actions
Set Temp_String = ((String(Temp_Integer)) + (, + Temp_String2))
Else - Actions
Game - Display to (All players) for 4.00 seconds the text: Temp_String
fully tested and works![]()