AppleScript - tamsayı uzunluğu nasıl sayılır?


2

Bu yüzden 0 ile 9999 arasında her sayıyı yazacak olan bu programı hazırlıyordum, ancak rakam sayısı ne olursa olsun 4 karakter uzunluğunda olmasına ihtiyacım var - bunu yapmanın tek yolu , programın sayım yapmasıdır. rakamlar ve ardından bir değişkene koyup, bu değişkeni 4'ten çıkartarak ve bu sayıyı sayının önüne koymadan önce ekler.

AppleScript rakamları nasıl sayabilir? Bunu yapmanın bir yolu var mı?

Yanıtlar:


3

İşte sorunuzu temel alan basit bir işleyici .

Örnek AppleScript kodu:

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

Sonuç: "0001"


Bir demo olarak, dolgunun gerçekleştiğini göstermek için 0 ile 1000 arasında dolgulu sayılar listesini oluşturur ve görüntüler.

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

4

Apple Geliştirici> Mac Otomasyon Komut Dosyası Kılavuzu, bir numaraya nasıl JavaScript kodunun yanı sıra AppleScript olarak başa sıfır ekleneceğini gösteren bir örnek içerir :

Sayıya Önde Gelen Sıfır Ekleme

Liste 20-11 ve Liste 20-12'deki işleyiciler, bir sayıyı dizeye dönüştürür ve belirli bir uzunluğa ulaşana kadar baştaki sıfırlarla hazırlar. İki parametreyi kabul ederler; satır başına sıfır eklemek için sayı ve eklenecek maksimum satır başına sıfır sayısı. Örneğin, maksimum satır başı sayısı 2 olarak ayarlanmışsa, sonuçlar 001 ila 999 arasındadır. Maksimum satır sayısı 3 ise, sonuçlar 0001 ila 9999 arasında değişir.

AppleScript listesi:

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

JavaScript'i tercih ederseniz bağlantıyı kontrol edin.


Bu cevabın basit bir if / else ifadesinin aksine herhangi bir sıfır için nasıl çalıştığını seviyorum.
SilverWolf
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.