5

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.

2 Answers 2

5

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 as 1.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
3
  • just tobe sure, will this work even when I need to work with it as a number afterwards? Commented Nov 2, 2017 at 20:30
  • @Prokop If you want to work with it as a number, just keep it as the original number and work with that until you want to output it as a string.
    – grg
    Commented Nov 2, 2017 at 20:35
  • a'ight, thanks. mind checking out my other unanswered question as well? Commented Nov 2, 2017 at 20:37
5

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.

2
  • What does inches etc add to the expression? I don't see any inch symbol " and when I myself test with different units the result is the same, independently of the unit.
    – d-b
    Commented Apr 15, 2023 at 15:47
  • @d-b Yes, that's correct. There's no symbol for any of these units. They're class object specifiers used for conversions. I chose inches as it has the fewest letters, but isn't otherwise special in and of itself. However, the act of coercing between types using a measurement unit class as an intermediary is special. As for why this would be or should be the case, I don't know. It may not be a feature that was intended to be part of AppleScript, but rather a bug whose consequences are advantageous rather than detrimental (it wouldn't be the only example of such a phenomenon).
    – CJK
    Commented May 21, 2023 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .