How do I prevent AppleScript from using short numbers?
So AppleScript by default uses the exponential notation of numbers - for example, 2.99E+68. How do I prevent that from happening? I want the number written out as it is.
Solution 1:
An AppleScript function convertNumberToString
is provided by Apple.
Mac Automation Scripting Guide: Manipulating Numbers
Converting a Long Number to a String
In AppleScript, long numeric values are displayed in scientific notation. For example,
1234000000
is displayed by a script as1.234E+9
. When this value is coerced to a string, it becomes:"1.234E+9"
. The handler in Listing 20-3 converts a number, regardless of length, to a string of numeric characters instead of a numeric string in scientific notation.on convertNumberToString(theNumber) set theNumberString to theNumber as string set theOffset to offset of "E" in theNumberString if theOffset = 0 then return theNumberString set thePrefix to text 1 thru (theOffset - 1) of theNumberString set theConvertedNumberPrefix to "" if thePrefix begins with "-" then set theConvertedNumberPrefix to "-" if thePrefix = "-" then set thePrefix to "" else set thePrefix to text 2 thru -1 of thePrefix end if end if set theDecimalAdjustment to (text (theOffset + 1) thru -1 of theNumberString) as number set isNegativeDecimalAdjustment to theDecimalAdjustment is less than 0 if isNegativeDecimalAdjustment then set thePrefix to (reverse of (characters of thePrefix)) as string set theDecimalAdjustment to -theDecimalAdjustment end if set theDecimalOffset to offset of "." in thePrefix if theDecimalOffset = 0 then set theFirstPart to "" else set theFirstPart to text 1 thru (theDecimalOffset - 1) of thePrefix end if set theSecondPart to text (theDecimalOffset + 1) thru -1 of thePrefix set theConvertedNumber to theFirstPart set theRepeatCount to theDecimalAdjustment if (length of theSecondPart) is greater than theRepeatCount then set theRepeatCount to length of theSecondPart repeat with a from 1 to theRepeatCount try set theConvertedNumber to theConvertedNumber & character a of theSecondPart on error set theConvertedNumber to theConvertedNumber & "0" end try if a = theDecimalAdjustment and a is not equal to (length of theSecondPart) then set theConvertedNumber to theConvertedNumber & "." end repeat if theConvertedNumber ends with "." then set theConvertedNumber to theConvertedNumber & "0" if isNegativeDecimalAdjustment then set theConvertedNumber to (reverse of (characters of theConvertedNumber)) as string return theConvertedNumberPrefix & theConvertedNumber end convertNumberToString
Solution 2:
You can coerce the number to a unit of measurement and then into a string, like this:
1.2345E+9 as inches as string
which will return "1234500000"
.
You can use any unit of measurement, such as kilograms, cubic centimetres, gallons, degrees Kelvin etc.