Comma Delimit a Number

The full expression used is '$‘\n’', which gets bash to insert a literal linefeed into the sed script. The first backslash is required by sed. The first single-quote comes out of the quoted sed code. The $‘\n’ is the bash linefeed. The last quote goes back into the quoted code.

I did this because sed’s loop markers don’t like semicolons after them, for some reason. I could of course just have typed linefeeds at those points. :wink:

Interesting stuff!

Late as usual…

Here’s a simple one you can use in an ASObjC-based library under Mavericks:

use framework "Foundation"

on formatNumber:theNumber
	set theFormatter to current application's NSNumberFormatter's new()
	theFormatter's setNumberStyle:(current application's NSNumberFormatterDecimalStyle)
	set theResult to theFormatter's stringFromNumber:theNumber
	return theResult as text
end formatNumber:

This is faster than the sed version, but slower than MacUserII’s. However, that version assumes you’re starting of with a string like “1234567890123456789”, and my guess is that the OP is starting with a number – so it really needs to be prepared to turn 1.23456789012346E+18 into a usable string.

The other advantage it has is that it will also make Yvan happy – it will use the thousands separator the user has specified on their computer.

And for readers not sure of what that involves, see macscripter.net/viewtopic.php?pid=168044#p168044

Hello.

I’m even later, I just wrote one to return the thousand separator only, while I figure out how to get the list separator, without going into the locale with a do shell script.

framework "Foundation"
on thouSep()
	tell current application's NSNumberFormatter's new()'s thousandSeparator()
		return it as string
	end tell
end thouSep
thouDelimit("123456789012345678901234567", ",")

on thouDelimit(n, delim)
	set s to ""
	repeat until (count of n) < 4
		set s to delim & (text -3 thru -1 of n) & s
		set n to text 1 thru -4 of n
	end repeat
	return n & s
end thouDelimit

edit: I overlooked Nigel’s post. It seems that his and mine are almost the same only I wrote it down in fewer lines of code.