INDEX>Foundation>NSDecimalNumber
丸めた十進数オブジェクトを返します
-(NSDecimalNumber *)decimalNumberByRoundingAccordingToBehavior:(id <NSDecimalNumberBehaviors>)behavior:
解説
丸めた十進数オブジェクトを返します。
レシーバが3.141592でデフォルトの丸めを使えば3.14を返します。
フレームワーク
Foundation
クラス
NSDecimalNumber
使用可能
10.0
例文
#import "MyObject.h" @implementation MyObject - (IBAction)myAction:(id)sender { NSNumberFormatter *numFormat = [[NSNumberFormatter alloc] init] ; NSDecimalNumber *dNum1 = [[[NSDecimalNumber alloc] initWithString:@"3.141592"] autorelease]; NSDecimalNumber *dNum = [NSDecimalNumber alloc] ; dNum = [dNum1 decimalNumberByRoundingAccordingToBehavior:[NSDecimalNumber defaultBehavior]]; [numFormat setMaximum:dNum]; [numFormat setPositiveFormat:@"#,##0.00"]; [myOutlet setFormatter:numFormat]; NSLog([NSString stringWithFormat:@"%.2f",[[numFormat maximum] doubleValue]]); } @end
//NSDecimalNumber decimalNumberByRoundingAccordingToBehavior: @IBAction func function022(sender: AnyObject) { //カスタムの丸め挙動 小数点第二位でRoundPlain let behavior : NSDecimalNumberHandler = NSDecimalNumberHandler(roundingMode: NSRoundingMode.RoundPlain, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) let dNum1 : NSDecimalNumber = NSDecimalNumber(string: "3.141592") let dNum : NSDecimalNumber = dNum1.decimalNumberByRoundingAccordingToBehavior(behavior) let numFormat : NSNumberFormatter = NSNumberFormatter() numFormat.minimum = dNum //最小値が3.14000 numFormat.positiveFormat = "#,##0.000000" textField.formatter = numFormat NSLog("%f",numFormat.minimum!.floatValue) commentLabel.stringValue = "←最小値が3.14000" }
編集時のバージョン
OS X 10.11
Swift2.0