Select Page
Latest 0.1.1 https://github.com/JyHu/Number MIT ios 7.0 JyHu

使用

• 加法
• 减法
• 乘法
• 除法
• 求方
• 乘以10的n次方
• 平方
• 立方
• 绝对值
• 四舍五入
• 字符串带精度转换

简单计算

``````NSString *a = @"10.2";
NSNumber *b = @20.3;
NSDecimalNumber *c = [[NSDecimalNumber alloc] initWithFloat:30.4];

NSDecimalNumber *res = a.add(b).multiplying(c);
NSString *resStr = res.numberStringWithScale(3);

NSLog(@"res %@   resStr %@", res.stringValue, resStr);

// 结果：  res 927.19999   resStr 927.200``````

数值转换

• 科学计数法`1,340,100`
• 期货里的利率合约的价格`200'384`

``````[AUUNumberHandler globalNumberStringRefactorWithNumber:^id(NSString *numberString) {
NSString *fac = numberString;
if ([fac containsString:@","]) {
fac = [fac stringByReplacingOccurrencesOfString:@"," withString:@""];
}

if ([fac containsString:@"'"]) {
fac = [fac stringByReplacingOccurrencesOfString:@"." withString:@""];
}

if (fac.isPureFloatString) {
return fac;
}

return @1;
}];``````

容错处理

• `a.add(b)`
• `(@1).add(nil)`

``````[AUUNumberHandler globalNumberHandler:^(AUUNumberHandler *numberHandler) {
numberHandler.roundingScale = 5;
numberHandler.mode = NSRoundPlain;
numberHandler.raiseOnOverflow = YES;
} exceptionHandler:^NSDecimalNumber *(SEL operation, NSCalculationError error, NSDecimalNumber *leftOperand, NSDecimalNumber *rightOperant) {
return (@1).decimalNumber;
}];``````

扩展

Latest podspec

```{
"name": "Number",
"version": "0.1.1",
"summary": "u4e00u4e2au8ba1u7b97u65b9u4fbfu3001u5bb9u6613u6269u5c55u7684u6570u503cu8ba1u7b97u5e93u3002",
"description": "u4e00u4e2au7b80u5355u7684u6570u503cu8ba1u7b97u7684u5e93uff0cu5c01u88c5u4e86u7cfbu7edfu7684NSDecimalNumberuff0cu65b9u4fbfu6570u503cu8ba1u7b97uff0cu907fu514du7cbeu5ea6u7684u4e22u5931u3002u4f7fu7528u7684u65f6u5019u53efu4ee5u76f4u63a5u5bf9NSStringu3001NSNumberu3001NSDecimalNumberu7c7bu578bu7684u6570u505au6570u503cu7684u52a0u51cfu4e58u9664u7b49u8fd0u7b97u3002",
"homepage": "https://github.com/JyHu/Number",
"type": "MIT",
},
"authors": {
"JyHu": "[email protected]"
},
"source": {
"git": "https://github.com/JyHu/Number.git",
"tag": "0.1.1"
},
"platforms": {
"ios": "7.0"
},
"source_files": "AUUNumber/*.{h,m}"
}```