AppleScript - come contare la lunghezza intera?

2

Quindi stavo creando un programma che scrivesse ogni numero compreso tra 0 e 9999, ma ho bisogno che sia lungo 4 caratteri, indipendentemente dal numero di cifre, l'unico modo per farlo, penso sia fai in modo che il programma contenga le cifre e poi lo inserisca in una variabile, sottraendo tale variabile da 4 e aggiungendo quel numero di zeri prima del numero stesso.

In che modo AppleScript può contare le cifre? C'è persino un modo per farlo?

    
posta Prokop Hanzl 22.09.2017 - 19:48
fonte

2 risposte

3

Ecco un semplice gestore che, pad basati sulla tua domanda.

Codice AppleScript di esempio:

set theNumber to 1

set theNumber to padNumberAsString(theNumber)

on padNumberAsString(n)
    set theLength to (get length of (n as string))
    if theLength is equal to 1 then
        set n to "000" & n
    else if theLength is equal to 2 then
        set n to "00" & n
    else if theLength is equal to 3 then
        set n to "0" & n
    end if
    return n
end padNumberAsString

Result: "0001"

Come demo, questo crea e visualizza un elenco dei numeri riempiti tra 0 e 1000 per mostrare che il padding è in corso.

set thePaddedList to {}
repeat with i from 0 to 1000
    set end of thePaddedList to padNumberAsString(i)
end repeat
choose from list thePaddedList

on padNumberAsString(n)
    set theLength to (get length of (n as string))
    if theLength is equal to 1 then
        set n to "000" & n
    else if theLength is equal to 2 then
        set n to "00" & n
    else if theLength is equal to 3 then
        set n to "0" & n
    end if
    return n
end padNumberAsString
    
risposta data 22.09.2017 - 20:53
fonte
4

Sviluppatore Apple > Mac Automation Scripting Guide contiene un esempio come aggiungere zeri iniziali a un numero come codice AppleScript e JavaScript:

Adding Leading Zeros to a Number

The handlers in Listing 20-11 and Listing 20-12 convert a number to a string and prepends it with leading zeros until it reaches a certain length. They accept two parameters—the number to add leading zeros to and the maximum number of leading zeros to add. For example, if the maximum number of leading zeros is set to 2, the results range from 001 to 999. If the maximum number of leading zeros is 3, the results range from 0001 to 9999, and so on.

Elenco AppleScript:

on addLeadingZerosToNumber(theNumber, theMaxLeadingZeroCount)
    -- Determine if the number is negative
    set isNegative to theNumber is less than 0

    -- Determine when the maximum number of digits will be reached
    set theThreshold to (10 ^ theMaxLeadingZeroCount) as integer

    -- If the number is shorter than the maximum number of digits
    if theNumber is less than theThreshold then
        -- If the number is negative, convert it to positive
        if isNegative = true then set theNumber to -theNumber

        -- Add the zeros to the number
        set theLeadingZeros to ""
        set theDigitCount to length of ((theNumber div 1) as string)
        set theCharacterCount to (theMaxLeadingZeroCount + 1) - theDigitCount
        repeat theCharacterCount times
            set theLeadingZeros to (theLeadingZeros & "0") as string
        end repeat

        -- Make the number negative, if it was previously negative
        if isNegative = true then set theLeadingZeros to "-" & theLeadingZeros

        -- Return the prefixed number
        return (theLeadingZeros & (theNumber as text)) as string

      -- If the number is greater than or equal to the maximum number of digits
    else
        -- Return the original number
        return theNumber as text
    end if
end addLeadingZerosToNumber

Se preferisci JavaScript controlla il link.

    
risposta data 22.09.2017 - 20:17
fonte

Leggi altre domande sui tag