iOS 字符串和浮点型相互转换问题

  1. 浮点型转字符串 金融遇到只保留小数位不进行四舍五入的时候

//浮点型转字符串,不进行四舍五入 参数:浮点型数值,保留的小数位数
+(NSString *)notRounding:(double)dValue afterPoint:(int)position{

NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber *ouncesDecimal;
NSDecimalNumber *roundedOunces;

ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:dValue];
roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];

return [NSString stringWithFormat:@"%@",roundedOunces];

}

2.字符串转浮点型 floatValue,doubleValue 造成的实际偏差
解决办法,下面两个分类,nsdecimal对象
.h文件
//
// NSDecimalNumber+Addtion.h
// 有应用应用
//
// Created by xuliying on 15/10/15.
// Copyright (c) 2015年 xly. All rights reserved.
//

import

typedef NS_ENUM(NSInteger, type) {
Add,
subtract,
multiply,
divide
};
@interface NSDecimalNumber (Addtion)
+(NSDecimalNumber *)aDecimalNumberWithString:(NSString *)num1 type:(type)type anotherDecimalNumberWithString:(NSString *)num2;
+(NSComparisonResult)aDecimalNumberWithString:(NSString *)num1 compareAnotherDecimalNumberWithString:(NSString *)num2;

+(NSDecimalNumber *)aDecimalNumberWithNumber:(NSNumber *)num1 type:(type)type anotherDecimalNumberWithNumber:(NSNumber *)num2;
+(NSComparisonResult)aDecimalNumberWithNumber:(NSNumber *)num1 compareAnotherDecimalNumberWithNumber:(NSNumber *)num2;
@end

.m文件
//
// NSDecimalNumber+Addtion.m
// 有应用应用
//
// Created by xuliying on 15/10/15.
// Copyright (c) 2015年 xly. All rights reserved.
//

import "NSDecimalNumber+Addtion.h"

@implementation NSDecimalNumber (Addtion)
+(NSDecimalNumber *)aDecimalNumberWithString:(NSString *)num1 type:(type)type anotherDecimalNumberWithString:(NSString *)num2{
if (type == Add) {
return [[NSDecimalNumber decimalNumberWithString:num1] decimalNumberByAdding:[NSDecimalNumber decimalNumberWithString:num2]];
}else if (type == subtract){
return [[NSDecimalNumber decimalNumberWithString:num1] decimalNumberBySubtracting:[NSDecimalNumber decimalNumberWithString:num2]];
}else if (type == multiply){
return [[NSDecimalNumber decimalNumberWithString:num1] decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:num2]];
}else if (type == divide){
return [[NSDecimalNumber decimalNumberWithString:num1] decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:num2]];
}else{
return nil;
}

}
+(NSComparisonResult)aDecimalNumberWithString:(NSString *)num1 compareAnotherDecimalNumberWithString:(NSString *)num2{
return [[NSDecimalNumber decimalNumberWithString:num1] compare:[NSDecimalNumber decimalNumberWithString:num2]];
}

+(NSDecimalNumber *)aDecimalNumberWithNumber:(NSNumber *)num1 type:(type)type anotherDecimalNumberWithNumber:(NSNumber *)num2{
if (type == Add) {
return [[NSDecimalNumber decimalNumberWithDecimal:[num1 decimalValue]] decimalNumberByAdding:[NSDecimalNumber decimalNumberWithDecimal:[num2 decimalValue]]];
}else if (type == subtract){
return [[NSDecimalNumber decimalNumberWithDecimal:[num1 decimalValue]] decimalNumberBySubtracting:[NSDecimalNumber decimalNumberWithDecimal:[num2 decimalValue]]];
}else if (type == multiply){
return [[NSDecimalNumber decimalNumberWithDecimal:[num1 decimalValue]] decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithDecimal:[num2 decimalValue]]];
}else if (type == divide){
return [[NSDecimalNumber decimalNumberWithDecimal:[num1 decimalValue]] decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithDecimal:[num2 decimalValue]]];
}else{
return nil;
}
}
+(NSComparisonResult)aDecimalNumberWithNumber:(NSNumber *)num1 compareAnotherDecimalNumberWithNumber:(NSNumber *)num2{
return [[NSDecimalNumber decimalNumberWithDecimal:[num1 decimalValue]] compare:[NSDecimalNumber decimalNumberWithDecimal:[num2 decimalValue]]];
}
@end

你可能感兴趣的:(iOS 字符串和浮点型相互转换问题)