AppleScript Math

小数点以下桁揃え

計算する場合

【スクリプトエディタで開く】|




log doRound2Dec({42.56834, 2, "up"})
log doRound2Dec({42.56894, 3, "down"})

##########################################
# 小数点以下桁揃え  数値 桁数 四捨五入 up or down
##########################################
to doRound2Dec({argNumNumber, argNumDecNo, argStrRound})
  set numNumber to argNumNumber as number
  set numDecNo to argNumDecNo as integer
  set strRound to argStrRound as text
  # 10のx乗 
  set numDec to (10 ^ numDecNo) as integer
  # ↑の数桁数だけ元の数値の桁数を増やして
  set numStep1 to (numNumber * numDec) as number
  # 数値の丸め 繰り上がりup と繰り下がりdownの処理
  if strRound is "up" then
    set numStep2 to (round of numStep1 rounding up) as integer
  else if strRound is "down" then
    set numStep2 to (round of numStep1 rounding down) as integer
  end if
  # 最後の桁数を戻して
  set numStep3 to (numStep2 / numDec) as number
  # テキストで戻す場合
  # set strStep3 to (numStep2 / numDec) as text
  # 数値で戻す
return numStep3
end doRound2Dec

テキストとして処理する場合

【スクリプトエディタで開く】|

set numNumber to 1024.126683 as number

log doRound2Dec(numNumber)

to doRound2Dec(argNumber)
  set numNumber to argNumber as number
  set strNumber to numNumber as text
  set AppleScript's text item delimiters to "."
  set listArgNumbar to every text item of strNumber as list
  set AppleScript's text item delimiters to ""
  #
  set numIntNo to text item 1 of listArgNumbar as integer
  set strIntNo to numIntNo as text
  #
  if (count of listArgNumbar) = 1 then
    set strDecNo to "00" as text
  else
    set numDec to numNumber - numIntNo as number
    set strDecNo to text item 2 of listArgNumbar as text
  end if
  set strDecimal to (text 1 through 2 of (strDecNo & "00")) as text
  #
  set strReturnNo to (strIntNo & "." & strDecimal) as text
  set strReturnNo to strReturnNo as text
return strReturnNo
end doRound2Dec

|

ファイルサイズ表記


【スクリプトエディタで開く】|


log doGetFileSize(6410)
log doGetFileSize("916,564,470")
log doGetFileSize("1,504,749,906")

####################################
# ファイルサイズ表記 入力は小数点の無いバイト
####################################
to doGetFileSize(argNumByteSize)
  #念の為カンマを取る
  set strNumber to doDelComma2Text(argNumByteSize) as text
  #受け取り値を数値で確定
  set numFileSize to strNumber as integer
  # バイト単位 10.5以前やWindowsターゲットの場合は1024に
  set numByteUnits to 1000 as integer
  # 単位分岐
  if numFileSize < numByteUnits then
    # log "b:バイト単位"
    set strFileSize to doRound2Dec(numFileSize) as text
    set strFileSize to (strFileSize & " B") as text
  else if numFileSize < (numByteUnits * numByteUnits) then
    # log "kb:キロバイト単位"
    set strFileSize to doRound2Dec(numFileSize / numByteUnits) as text
    set strFileSize to (strFileSize & " KB") as text
  else if numFileSize < (numByteUnits * numByteUnits * numByteUnits) then
    # log "mb:メガバイト単位"
    set strFileSize to doRound2Dec(numFileSize / (numByteUnits * numByteUnits)) as text
    set strFileSize to (strFileSize & " MB") as text
  else if numFileSize < (numByteUnits * numByteUnits * numByteUnits * numByteUnits) then
    # log "gb:ギガバイト単位"
    set strFileSize to doRound2Dec(numFileSize / (numByteUnits * numByteUnits * numByteUnits)) as text
    set strFileSize to (strFileSize & " GB") as text
  end if
return strFileSize
end doGetFileSize

####################################
# 小数点以下桁数
####################################
to doRound2Dec(argNumber)
  ## 100で小数点2位 10で1位 1000で3位まで計算
  set numDec to 100 as integer
  set strDecNo to ((round (argNumber * numDec)) / numDec) as text
return strDecNo
end doRound2Dec

####################################
# 数値のカンマ表記を取る
####################################
to doDelComma2Text(argStrNumber)
  set strNumber to argStrNumber as text
  set AppleScript's text item delimiters to ","
  set listDelim to every text item of strNumber
  set AppleScript's text item delimiters to ""
  set strNumber to (listDelim) as text
return strNumber
end doDelComma2Text



|

小数点以下2桁を数値で


【スクリプトエディタで開く】|

set numVar to 72 as number
set numIndex to 1.3426789 as number

###小数点以下2桁 切り捨て
set numStep1 to (numVar * numIndex) as number
log numStep1
set numStep2 to numStep1 * 100 as number
log numStep2
set intStep3 to (round of numStep2 rounding down) as integer
log intStep3
set numStep4 to intStep3 / 100 as number
log numStep4
-->(*96.67*)

###小数点3桁以降切り捨て式
set numAns to ((round of ((numVar * numIndex) * 100) rounding down) / 100) as number
log numAns
--> (*96.67*)

###小数点3桁以降切り上げ式
set numAns to ((round of ((numVar * numIndex) * 100) rounding up) / 100) as number
log numAns
--> (*96.68*)

###ままの値
set numStep1 to (numVar * numIndex) as number
log numStep1
--> (*96.6728808*)

|

[Decimal to Octal]10進数→8進数 (パーミンション番号用)


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application


log doOct2Dec(770)
log doDec2Oct(493)

###################################
## パーミッション 8進→10進
## Octal to Decimal
###################################

to doOct2Dec(argOctNo)
  set strOctalText to argOctNo as text
  set num3Line to (first item of strOctalText) as number
  set num2Line to (2nd item of strOctalText) as number
  set num1Line to (last item of strOctalText) as number
  set numDecimal to (num3Line * 64) + (num2Line * 8) + (num1Line * 1) as number
return numDecimal as number
end doOct2Dec

###################################
## パーミッション10進数 →8進数  
## Decimal to Octal
###################################
to doDec2Oct(argDecNo)
  set numDecNo to argDecNo as number
  set numDiv1 to (numDecNo div 8) as number
  set numMod1 to (numDecNo mod 8) as number
  set numDiv2 to (numDiv1 div 8) as number
  set numMod2 to (numDiv1 mod 8) as number
  set numDiv3 to (numDiv2 div 8) as number
  set numMod3 to (numDiv2 mod 8) as number
  set strOctal to (numMod3 & numMod2 & numMod1) as text
  set numOctal to strOctal as number
return numOctal as number
  
end doDec2Oct





|

ランダム番号生成


【スクリプトエディタで開く】|

set strStartNo to "10000000" as text
set strEndNo to "99999999" as text
set theParagraph to (random number from (strStartNo as integer) to (strEndNo as integer)) as integer


|

mod 割り算のあまり

【スクリプトエディタで開く】


set numCntLine to 0 as integer
set numCntRow to 0 as integer

repeat 25 times
  log numCntLine
  log numCntRow
  set numCntLine to numCntLine + 1 as integer
  ###5で割ってあまりが0の時だけ処理
  if (numCntLine mod 5) = 0 then
    set numCntRow to numCntRow + 1 as integer
  end if
end repeat

|

[8進数]8進数を10進数に

他にいい方法が思いつかない…
出来そうなのになぁ

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application


set strOctalText to "777" as text

set num3Line to first item of strOctalText as number
set num2Line to 2nd item of strOctalText as number
set num1Line to last item of strOctalText as number

set numDecimal to (num3Line * 64) + (num2Line * 8) + (num1Line * 1)

|

[四捨五入]小数点以下の桁数指定

set numDecNo to "123.45678" as number

####小数点以下1桁
set strDecNo to ((round (numDecNo * 10)) / 10) as text
-->"123.5"

####小数点以下2桁
set strDecNo to ((round (numDecNo * 100)) / 100) as text
-->"123.46"

####小数点以下3桁
set strDecNo to ((round (numDecNo * 1000)) / 1000) as text
-->"123.457"

|

[四捨五入]小数点以下の桁数(テキストとして扱う)

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

set numDecNo to "123.45678" as text

set ocidDecimalNumber to (refMe's NSDecimalNumber's decimalNumberWithString:numDecNo)
log ocidDecimalNumber as text
log className() of ocidDecimalNumber as text

###NSNumberFormatterの初期化
set ocidNSNumberFormatter to refMe's NSNumberFormatter's alloc()'s init()
log className() of ocidNSNumberFormatter as text
###小数点以下2桁のフォーマット
(ocidNSNumberFormatter's setPositiveFormat:"##0.00")
####フォーマットを適応
set ocidDecimalNumber to (ocidNSNumberFormatter's stringFromNumber:ocidDecimalNumber)
log ocidDecimalNumber as text
log className() of ocidDecimalNumber as text
log ocidDecimalNumber as text
-->(*123.46*)

|

[AppleScriptBasic]四捨五入

####四捨五入 切り上げ
set numNO to 1.12345 as number
log (round of numNO)
--> 1
set numNO to 1.47 as number
log (round of numNO)
--> 1
set numNO to 1.5 as number
log (round of numNO)
--> 2
####切り捨て
set numNO to 1.12345 as number
log (round of numNO rounding down)
--> 1
set numNO to 1.47 as number
log (round of numNO rounding down)
--> 1
set numNO to 1.5 as number
log (round of numNO rounding down)
--> 1
####切り上げ
set numNO to 1.12345 as number
log (round of numNO rounding up)
--> 2
set numNO to 1.47 as number
log (round of numNO rounding up)
--> 2
set numNO to 1.5 as number
log (round of numNO rounding up)
--> 2
return
###integer整数化
set numNO to 1.12345 as number
log (numNO) as integer
--> 1
set numNO to 1.47 as number
log (numNO) as integer
--> 1
set numNO to 1.5 as number
log (numNO) as integer
--> 2
####アメリカ式?
round 74.5678 rounding as taught in school
--> 75
round 74.4321 rounding as taught in school
--> 74
round 74.49 rounding as taught in school
--> 74

|

より以前の記事一覧

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom