Archive for the 'NSNumberFormatterRoundUp' Category

2017/08/28 数値文字列に対して、少数点以下の数値の切り上げ、切り下げ v2

数値文字列(numeric string)に対して、少数点以下の数値の切り上げ、切り下げを行うAppleScriptの改修版です(Thanks Shane!)。

REST APIから受け取ることの多い数値文字列(Numeric String)の切り下げを行うために作成したものですが、Shaneいわく「floatよりもdoubleのほうが精度が高いのでdoubleのほうがいいよ」とのこと。

また、stringFromNumber:で直接doubleValueを渡して大丈夫とのことだったので、いろいろ試しつつ(変更前と比較しつつ)、だいたいの目的である小数点以下2桁程度の数値文字列の切り上げについては挙動に変更はありません。

AppleScript名:数値文字列に対して、少数点以下の数値の切り上げ、切り下げ v2
– Created 2017-08-12 by Takaaki Naganoya
– Modified 2017-08-28 by Shane Stanley
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
–http://piyocast.com/as/archives/4793

property NSString : a reference to current application’s NSString
property NSNumber : a reference to current application’s NSNumber
property NSNumberFormatter : a reference to current application’s NSNumberFormatter
property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp
property NSNumberFormatterRoundDown : a reference to current application’s NSNumberFormatterRoundDown

set a to “0.9096617698669434″
set a1Res to roundingDownNumStr(a, 2) of me

set b to “0.0001830748806241899″
set b1Res to roundingDownNumStr(b, 2) of me

return {a1Res, b1Res}
–>  {0.9, 0.0}

on roundingDownNumStr(aNum as string, aDigit as integer)
  set a to NSString’s stringWithString:aNum
  
set aa to a’s doubleValue()
  
set aFormatter to NSNumberFormatter’s alloc()’s init()
  
aFormatter’s setMaximumFractionDigits:aDigit
  
aFormatter’s setRoundingMode:(NSNumberFormatterRoundDown)
  
set aStr to aFormatter’s stringFromNumber:aa
  
return (aStr as text) as real
end roundingDownNumStr

on roundingUpNumStr(aNum as string, aDigit as integer)
  set a to NSString’s stringWithString:aNum
  
set aa to a’s doubleValue()
  
set aFormatter to NSNumberFormatter’s alloc()’s init()
  
aFormatter’s setMaximumFractionDigits:aDigit
  
aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp)
  
set aStr to aFormatter’s stringFromNumber:aa
  
return (aStr as text) as real
end roundingUpNumStr

★Click Here to Open This Script 

2017/08/27 数値文字列に対して、少数点以下の数値の切り上げ、切り下げ

数値文字列(numeric string)に対して、少数点以下の数値の切り上げ、切り下げを行うAppleScriptです。

REST APIで桁数の多い数値文字列を返してくるものがあるので、その変換や桁数指定のために作成したものです。指定桁での数値の切り上げ、切り下げを処理してから数値に変換しないと指数表示になっていまひとつ取り回しが良くなかったので、数値文字列の状態で切り上げ、切り下げを行うようにしてみました。

AppleScript名:数値文字列に対して、少数点以下の数値の切り上げ、切り下げ
– Created 2017-08-12 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
–http://piyocast.com/as/archives/4791

property NSString : a reference to current application’s NSString
property NSNumber : a reference to current application’s NSNumber
property NSNumberFormatter : a reference to current application’s NSNumberFormatter
property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp
property NSNumberFormatterRoundDown : a reference to current application’s NSNumberFormatterRoundDown

set a to “0.9096617698669434″
set a1Res to roundingDownNumStr(a, 2) of me
–>  0.9

set b to “0.0001830748806241899″
set b1Res to roundingDownNumStr(b, 2) of me
–>  0.0

on roundingDownNumStr(aNum as string, aDigit as integer)
  set a to NSString’s stringWithString:aNum
  
set aa to a’s floatValue()
  
set aFormatter to NSNumberFormatter’s alloc()’s init()
  
aFormatter’s setMaximumFractionDigits:aDigit
  
aFormatter’s setRoundingMode:(NSNumberFormatterRoundDown)
  
set aStr to aFormatter’s stringFromNumber:(NSNumber’s numberWithFloat:aa)
  
return (aStr as text) as real
end roundingDownNumStr

on roundingUpNumStr(aNum as string, aDigit as integer)
  set a to NSString’s stringWithString:aNum
  
set aa to a’s floatValue()
  
set aFormatter to NSNumberFormatter’s alloc()’s init()
  
aFormatter’s setMaximumFractionDigits:aDigit
  
aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp)
  
set aStr to aFormatter’s stringFromNumber:(NSNumber’s numberWithFloat:aa)
  
return (aStr as text) as real
end roundingUpNumStr

★Click Here to Open This Script