INDEX>Foundation>NSDecimalNumber
十進数オブジェクトを乗算した結果を返します
- (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber withBehavior:(id<NSDecimalNumberBehaviors>)behavior
func decimalNumberByMultiplyingBy(_ decimalNumber: NSDecimalNumber, withBehavior behavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber
解説
十進数オブジェクトを乗算した結果を返します。
計算誤差の丸め(四捨五入・切り上げ・切り捨て)はbehaviorを使います。
引数
( NSDecimalNumber * )decimalNumber
_ decimalNumber: NSDecimalNumber
十進数オブジェクト
( id <NSDecimalNumberBehaviors> )behavior
,withBehavior behavior: NSDecimalNumberBehaviors?
丸め方法
フレームワーク
Foundation
クラス
NSDecimalNumber
使用可能
10.0
参照
例文
#import "MyObject.h" @implementation MyObject - (IBAction)myAction:(id)sender { NSNumberFormatter *numFormat = [[NSNumberFormatter alloc] init] ; NSDecimalNumber *dNum1 = [[[NSDecimalNumber alloc] autorelease] initWithString:@"3000"]; NSDecimalNumber *dNum2 = [[[NSDecimalNumber alloc] autorelease] initWithString:@"200"]; NSDecimalNumber *dNum = [[NSDecimalNumber alloc] autorelease]; dNum = [dNum1 decimalNumberByMultiplyingBy:dNum2 withBehavior:[NSDecimalNumber defaultBehavior]]; [numFormat setMaximum:dNum]; [numFormat setPositiveFormat:@"#,##0.00"]; [myOutlet setFormatter:numFormat]; NSLog([NSString stringWithFormat:@"%.2f",[[numFormat maximum] doubleValue]]); } @end
//NSDecimalNumber decimalNumberByMultiplyingBy:withBehavior: @IBAction func function017(sender: AnyObject) { let dNum1 : NSDecimalNumber = NSDecimalNumber(string: "30") let dNum2 : NSDecimalNumber = NSDecimalNumber(string: "20") let dNum : NSDecimalNumber = dNum1.decimalNumberByMultiplyingBy(dNum2, withBehavior: NSDecimalNumber.defaultBehavior()) let numFormat : NSNumberFormatter = NSNumberFormatter() numFormat.minimum = dNum //最小値が600 numFormat.positiveFormat = "#,##0.00" textField.formatter = numFormat NSLog("function017") commentLabel.stringValue = "←最小値が30*20=600(defaultBehavior)" }
編集時のバージョン
OS X 10.11
Swift2.0