10進数の数字文字列に3桁セパレータを付加、外して数字文字列に戻すAppleScriptです。
めんどくさい上にCocoaに機能があり、かつそれほど大量のデータ処理を行うわけでもない(スピードを要求されない)処理のはずなので、手抜きでCocoaの機能を呼び出しています。
AppleScript名:数値に3桁セパレータを付加、外して数値に戻す v2 |
— Created 2016-10-12 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" –3桁セパレータ文字「,」をNSLocaleのcurrentLocaleから取得するように変更 property NSString : a reference to current application’s NSString property NSLocale : a reference to current application’s NSLocale property NSCharacterSet : a reference to current application’s NSCharacterSet property NSNumberFormatter : a reference to current application’s NSNumberFormatter property NSLocaleGroupingSeparator : a reference to current application’s NSLocaleGroupingSeparator property NSNumberFormatterDecimalStyle : a reference to current application’s NSNumberFormatterDecimalStyle set aNum to 100000000 set aStr to formatNum(aNum) of me –> "100,000,000" set bNum to deFromatNumStr(aStr) of me –> "100000000" on formatNum(theNumber as number) set theResult to NSNumberFormatter’s localizedStringFromNumber:theNumber numberStyle:(NSNumberFormatterDecimalStyle) return theResult as text end formatNum on deFromatNumStrAndRetNumber(theNumericString as string) set aThousandSep to NSLocale’s currentLocale()’s objectForKey:(NSLocaleGroupingSeparator) set notWantChars to NSCharacterSet’s characterSetWithCharactersInString:aThousandSep set targStr to NSString’s stringWithString:theNumericString set newStr to (targStr’s componentsSeparatedByCharactersInSet:notWantChars)’s componentsJoinedByString:"" return ((newStr as string) as number) –Danger in OS X 10.10 (floating point casting bug) end deFromatNumStrAndRetNumber on deFromatNumStr(theNumericString as string) set aThousandSep to NSLocale’s currentLocale()’s objectForKey:(NSLocaleGroupingSeparator) set notWantChars to NSCharacterSet’s characterSetWithCharactersInString:aThousandSep set targStr to NSString’s stringWithString:theNumericString set newStr to (targStr’s componentsSeparatedByCharactersInSet:notWantChars)’s componentsJoinedByString:"" return (newStr as string) end deFromatNumStr |
More from my site
(Visited 59 times, 1 visits today)