LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Formatting numeric stored as string to include thousand separator

    Employee
    Posted 08-22-2013 02:56

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: aop

    Hi!

    I'm looking for a trick to format numeric values I have in a string field to include a thousand separator (I will also keep them as strings).

    So I'm looking for this type result
    10->10
    1000->1 000
    10000000.98->10 000 000.98

    Any suggestions?


  • 2.  RE: Formatting numeric stored as string to include thousand separator

    Employee
    Posted 08-23-2013 08:15

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ejones

    I don't know of a trick. It seems to require good old fashioned programming using the BrainScript while loop, the div() and mod() functions.

    So if that value you want converted is in the variable n you could do this:
    a = split(format("%f",n),".")
    if a.len() > 1 
    	then {
    		decimal = "." + str(a[1])
    		while decimal.right(1) == "0" decimal = left(decimal,strlen(decimal)-1)
    	} else {
    		decimal = ""
    	}
    t = long(floor(n))
    if t >= 1000
    	then s = format("%03ld",t.mod(1000))
    	else s = format("%3ld",t.mod(1000))
    t = t.div(1000)
    while (t > 0) {
    	if t >= 1000
    		then s = format("%03ld %s",t.mod(1000),s)
    		else s = format("%3ld %s",t.mod(1000),s)
    	t = t.div(1000)
    }
    emit trim(s + decimal) as Converted