Formatting with stringWithFormat_

I am trying to use ‘stringWithFormat_’ to get formatted output of numeric values, but the output is very strange.

	set myNumber to 3.14155
	tell class "NSString" of current application
		set x to stringWithFormat_("%010.2f", myNumber)
	end tell
	tell me to log x

the result of this code is 0000000.00

            set myNumber to 125
	tell class "NSString" of current application
		set x to stringWithFormat_("%5d", myNumber)
	end tell
	tell me to log x

the result of this code is 3523392.

When i run the application again I get a different results (3799648) etc…

Is this the right way using this method or am i overlooking some details?

I hope somebody can help, thanks already!!!

According to Shane’s book," AppleScriptObjC Explored", you can’t use stringWithFormat_ in ASOC. You’ll have to use an NSNumberFormatter to do what you want.

Ric

Hello,

the code looks ok, but the format specifiers you are using are a bit strange looking… what do you wish to obtain exactly?

If you look at the “String Format Specifiers” in the document none of the available specifiers are used in your code. Are you sure you want this kind of formatter? If you need a number formatter, this is an example:

set number_formatter to current application's NSNumberFormatter's alloc()'s init()
tell number_formatter
	setFormat_("0.####")
	setRoundingMode_(7)
	set theRoundedValueAsString to stringFromNumber_(1.2345678) --returns "1.2346"
	set theRoundedValueAsReal to numberFromString_("1.2345678") as real --returns 1.2346
end tell

Would this be what you are looking for maybe?

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

That solves it. Well, number formatters and Date formatters work fine. If you need more info on either, please specify what you want the result to be, it will help.

Fred

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

According to the documentation, you can use this method to get a formatted string with a printf c-style (see string format specifiers in the xcode documentation).
I have been using this type of formatting with the the do shell script "printf … way with success.

Thanks for the answers so far !

The problem is inherent in the way the scripting bridge works. When you pass an integer or a real to a method, the bridge automatically converts it to an NSNumber object – they aren’t seen as integers or floats by Cocoa, so you can’t use the various tokens like %d and %f.

Everything from AppleScript gets turned into an object of some kind, so the only token you can use is %@. And that doesn’t give you a lot of options.

Thanks Shane,
It’s a pity but that’s the way it is. It anyhow spares me putting energy in something that doesn’t lead to a result.

I guess the good news is that, in terms of numbers, the formatters are much more capable.

Thanks Shane,
I indeed started using the number formatters.
I also wrote a simplified function to use the printf style in some cases:

For the interested people:
Comments are welcome

on printf_value_(formatstr, theValue)
	try
		set formatstr to formatstr as string
		if formatstr contains "i" or formatstr contains "u" or formatstr contains "x" then
			set theValue to theValue as integer
		else if formatstr contains "f" or formatstr contains "e" or formatstr contains "g" or formatstr contains "a" then
			set theValue to theValue as real
		else if formatstr contains "s" then
			set theValue to quoted form of (theValue as string)
		end if
		set formattedNumber to do shell script "printf " & formatstr & " " & theValue
		return formattedNumber as string
	on error errmsg
		return missing value
	end try
end printf_value_

I don’t quite understand what you’re trying to do with this method. If you want to use printf type formatting, why not just use “do shell script” directly. What does the rest of this code do for you?

Ric

I understand your comment, but such a mehod can be placed in a library and called in a structured way in different projects.
If changes have to be done, you only need to make the changes in one place.