NSDecimalNumber学习记录

来源:互联网 发布:淘宝如何找客户 编辑:程序博客网 时间:2024/06/03 12:06

NSDecimalNumber是NSNumber的子类,不可变,提供一个面向对象的方式来做十进制运算.是一个wrapper,我猜是有相应的CF函数的.一个实例可以表示任何数字在这个范围内(mantissa x 10^exponent)

> mantissa 定点部分  exponent 指数

定点部分是一个十进制数,最高38位;指数是一个整数,从-128到127.

iOS 2.0+
macOS 10.0+
tvOS 9.0+
watchOS 2.0+

Important

The Swift overlay to the Foundation framework provides the Decimal
structure, which bridges to the NSDecimalNumber class. The Decimal
value type offers the same functionality as the NSDecimalNumber
reference type, and the two can be used interchangeably in Swift code
that interacts with Objective-C APIs. This behavior is similar to how
Swift bridges standard string, numeric, and collection types to their
corresponding Foundation classes.

For more information about value types, see Classes and Structures in
The Swift Programming Language (Swift 3.0.1) and Working with Cocoa
Frameworks in Using Swift with Cocoa and Objective-C (Swift 3.0.1).

Creating a Decimal Number

++ decimalNumberWithDecimal:
快速创建一个十进制,参数是一个NSDecimal结构体
+ decimalNumberWithMantissa:exponent:isNegative:
用一个u long long定点数,一个short指数,一个bool正负
+ decimalNumberWithString:
自己看文档
+ decimalNumberWithString:locale:

Initializing a Decimal Number

  • initWithDecimal:
  • initWithMantissa:exponent:isNegative:
  • initWithString:.
  • initWithString:locale:

Performing Arithmetic(算法)

  • decimalNumberByAdding:
  • decimalNumberBySubtracting: (减法)
  • decimalNumberByMultiplyingBy:
  • decimalNumberByDividingBy:
  • decimalNumberByRaisingToPower:(幂)
  • decimalNumberByMultiplyingByPowerOf10:
    Multiplies the receiver by 10^power and returns the product, a newly created NSDecimalNumber object.
    余下的方法是上面的方法附带一个withBehavior:参数

Rounding Off 四舍五入

  • decimalNumberByRoundingAccordingToBehavior:

Accessing the Value

decimalValue
The decimal number’s value, expressed as an NSDecimal structure.
doubleValue
The decimal number’s closest approximate double value.
- descriptionWithLocale:

Comparing Decimal Numbers
- compare:
比较

0 0
原创粉丝点击