记在前面:其他工具类方法可查看我另一篇文章(我就是传送门)。
金融浮点工具类我就全部都贴出来吧,有多余的方法不需要再自己删除。
DecimalsTool.h
#import
// 浮点数据精度矫正工具
@interface DecimalsTool : NSObject
#pragma mark - 直接舍弃
/**
直接舍弃第三位小数,返回值保留两位小数(例: 1.5555>>>1.55)
*/
+ (double)abandonThird:(double)flt;
/**
保留全部小数,若小数最后一位为0则把0舍弃(例: 1.1111>>>1.1111 1.0000>>>1 )
*/
+ (NSString *)stringAllDecimals:(double)flt;
/**
若小数最后一位为0则把0舍弃,结果最少保留一位小数(例:1.0100>>>1.01 1.0000>>>1.0)
*/
+ (NSString *)stringLeastOneDecimals:(double)flt;
/**
若小数最后一位为0则把0舍弃,结果最少保留两位小数(例:1.0100>>>1.01 1.0000>>>1.00)
*/
+ (NSString *)stringLeastTwoDecimals:(double)flt;
/**
若小数最后一位为0则把0舍弃,结果最多保留两位小数(例:1.1111>>>1.11 1.0000>>>1)
*/
+ (NSString *)stringMostTwoDecimals:(double)flt;
#pragma mark 拓展方法
/**
加英文逗号分隔符(金额适用),至少保留两位小数(例:1000000000.0000>>>1,000,000,000.00 1000000000.00001>>>1,000,000,000.000001)
*/
+ (NSString *)addSemicolonLestTwoDecimals:(double)flt;
/**
加英文逗号分隔符(金额适用),保留两位小数(例:1000000000.0000>>>1,000,000,000.00)
*/
+ (NSString *)addSemicolonKeepDecimals:(double)flt;
/**
加英文逗号分隔符(金额适用),不保留小数(例:1000000000.0000>>>1,000,000,000)
*/
+ (NSString *)addSemicolonNoDecimals:(double)flt;
/**
添加单位“元”或“万元”(优惠券等福利型场景使用),保留最后1位不为0的小数,若少于1万最多保留2位小数
*/
+ (NSString *)addUnitWelfareDecimals:(double)flt;
/**
添加单位“元”或“万”(优惠券等福利型场景使用),保留最后1位不为0的小数,若少于1万最多保留2位小数
*/
+ (NSString *)addUnitWelfareDecimals2:(double)flt;
/**
添加单位“万”(通用方法),至少保留两位小数
@param subUnit 附加单位(例:subUnit = @"元",return @"xx元"或@"xx万元")
*/
+ (NSString *)addUnitCommonDecimals:(double)flt subUnit:(NSString *)subUnit;
/**
回报率精度处理 至少保留1位小数、小数超过4位,则从第5位四舍五入,最多保留4位
*/
+ (NSString *)rateDecimals:(double)flt;
@end
DecimalsTool.m
#import "DecimalsTool.h"
@implementation DecimalsTool
#pragma mark - 直接舍弃
/**
直接舍弃第三位小数,返回值保留两位小数(例: 1.5555>>>1.55)
*/
+ (double)abandonThird:(double)flt {
// 旧方法
// NSString *ds = [NSString stringWithFormat:@"%.8lf", flt];
// NSString *cs = [ds substringToIndex:ds.length-6];
// return [cs doubleValue];
// 新方法
return floor(flt*100)/100.0;
}
/**
保留全部小数,若小数最后一位为0则把0舍弃(例: 1.1111>>>1.1111 1.0000>>>1 )
*/
+ (NSString *)stringAllDecimals:(double)flt {
NSString *ds = [NSString stringWithFormat:@"%.8lf", flt];
NSString *cs = [ds substringToIndex:ds.length-2];
NSArray *array = [cs componentsSeparatedByString:@"."];
NSString *decimalStr = [array lastObject];
for (NSInteger i = decimalStr.length - 1; i >= 0; i--) {
NSString *tmpStr = [decimalStr substringWithRange:NSMakeRange(i, 1)];
if ([tmpStr isEqualToString:@"0"]) {
if (i==0) {
decimalStr = @"";
}else {
decimalStr = [decimalStr substringToIndex:i];
}
}else {
break;
}
}
if (decimalStr.length>0) {
return [NSString stringWithFormat:@"%@.%@", [array firstObject], decimalStr];
}else {
return [NSString stringWithFormat:@"%@", [array firstObject]];
}
}
/**
若小数最后一位为0则把0舍弃,结果最少保留一位小数(例:1.0100>>>1.01 1.0000>>>1.0)
*/
+ (NSString *)stringLeastOneDecimals:(double)flt {
NSString *ds = [self stringAllDecimals:flt];
NSArray *array = [ds componentsSeparatedByString:@"."];
if (array.count > 1) {
NSString *lastStr = [array lastObject];
if (lastStr.length > 2) {
lastStr = [lastStr substringToIndex:2];
}
return [NSString stringWithFormat:@"%@.%@", [array firstObject], lastStr];
}else {
return [NSString stringWithFormat:@"%@.0", [array firstObject]];
}
}
/**
若小数最后一位为0则把0舍弃,结果最少保留两位小数(例:1.0100>>>1.01 1.0000>>>1.00)
*/
+ (NSString *)stringLeastTwoDecimals:(double)flt {
NSString *ds = [self stringAllDecimals:flt];
NSArray *array = [ds componentsSeparatedByString:@"."];
if (array.count > 1) {
NSString *lastStr = [array lastObject];
if (lastStr.length == 1) {
return [NSString stringWithFormat:@"%@.%@0", [array firstObject], lastStr];
}
return [NSString stringWithFormat:@"%@.%@", [array firstObject], lastStr];
}else {
return [NSString stringWithFormat:@"%@.00", [array firstObject]];
}
}
/**
若小数最后一位为0则把0舍弃,结果最多保留两位小数(例:1.1111>>>1.11 1.0000>>>1)
*/
+ (NSString *)stringMostTwoDecimals:(double)flt {
NSString *ds = [self stringAllDecimals:flt];
NSArray *array = [ds componentsSeparatedByString:@"."];
if (array.count > 1) {
NSString *lastStr = [array lastObject];
if (lastStr.length > 2) {
lastStr = [lastStr substringToIndex:2];
}
return [NSString stringWithFormat:@"%@.%@", [array firstObject], lastStr];
}else {
return [array firstObject];
}
}
#pragma mark 拓展方法
/**
加英文逗号分隔符(金额适用),仅限整数部分
*/
+ (NSString *)addSemicolonNumber:(long)number {
NSString *intStr = [NSString stringWithFormat:@"%ld", number];
BOOL isNegative = NO;
if (number < 0) {
isNegative = YES;
intStr = [intStr substringFromIndex:1];
}
int section = (int)intStr.length / 3;
int remainder = (int)intStr.length % 3;
NSString *targetStr;
for (int i = 0; i <= section; i++) {
if (i == 0) {
if (remainder == 0) {
targetStr = [NSString stringWithFormat:@"%@",[intStr substringToIndex:remainder]];
}else{
targetStr = [NSString stringWithFormat:@"%@,",[intStr substringToIndex:remainder]];
}
}else{
targetStr = [NSString stringWithFormat:@"%@%@,", targetStr, [intStr substringWithRange:NSMakeRange(remainder + (i - 1) * 3, 3)]];
}
}
NSString *tmpStr = [targetStr substringToIndex:targetStr.length - 1];
if (isNegative) {
return [NSString stringWithFormat:@"-%@",tmpStr];
}
return tmpStr;
}
/**
加英文逗号分隔符(金额适用),至少保留两位小数(例:1000000000.0000>>>1,000,000,000.00 1000000000.00001>>>1,000,000,000.000001)
*/
+ (NSString *)addSemicolonLestTwoDecimals:(double)flt {
NSString *ds = [self stringLeastTwoDecimals:flt];
NSArray *array = [ds componentsSeparatedByString:@"."];
NSString *intStr = [self addSemicolonNumber:[[array firstObject] longValue]];
return [intStr stringByAppendingString:[NSString stringWithFormat:@".%@",[array lastObject]]];
}
/**
加英文逗号分隔符(金额适用),保留两位小数(例:1000000000.0000>>>1,000,000,000.00)
*/
+ (NSString *)addSemicolonKeepDecimals:(double)flt {
NSString *ds = [NSString stringWithFormat:@"%.2f", flt];
NSArray *array = [ds componentsSeparatedByString:@"."];
NSString *intStr = [self addSemicolonNumber:[[array firstObject] longValue]];
return [intStr stringByAppendingString:[NSString stringWithFormat:@".%@",[array lastObject]]];
}
/**
加英文逗号分隔符(金额适用),不保留小数(例:1000000000.0000>>>1,000,000,000)
*/
+ (NSString *)addSemicolonNoDecimals:(double)flt {
NSString *ds = [self addSemicolonKeepDecimals:flt];
return [ds substringToIndex:ds.length - 3];
}
/**
添加单位“元”或“万元”(优惠券等福利型场景使用),保留最后1位不为0的小数,若少于1万最多保留2位小数
*/
+ (NSString *)addUnitWelfareDecimals:(double)flt {
if (flt >= 10000) {
return [[DecimalsTool stringAllDecimals:flt/10000.0] stringByAppendingString:@"万元"];
}else {
return [[DecimalsTool stringMostTwoDecimals:flt] stringByAppendingString:@"元"];
}
}
+ (NSString *)addUnitWelfareDecimals2:(double)flt {
if (flt >= 10000) {
return [[DecimalsTool stringAllDecimals:flt/10000.0] stringByAppendingString:@"万"];
}else {
return [[DecimalsTool stringMostTwoDecimals:flt] stringByAppendingString:@"元"];
}
}
/**
添加单位“万”(通用方法),至少保留两位小数
@param subUnit 附加单位(例:subUnit = @"元",return @"xx元"或@"xx万元")
*/
+ (NSString *)addUnitCommonDecimals:(double)flt subUnit:(NSString *)subUnit {
NSString *string = nil;
if (flt >= 10000) {
string = [[DecimalsTool stringLeastTwoDecimals:flt/10000.0] stringByAppendingString:@"万"];
}else {
string = [DecimalsTool stringLeastTwoDecimals:flt];
}
if ([subUnit isKindOfClass:[NSString class]]) {
return [string stringByAppendingString:subUnit];
}else {
return string;
}
}
/**
回报率精度处理 至少保留1位小数、小数超过4位,则从第5位四舍五入,最多保留4位
*/
+ (NSString *)rateDecimals:(double)flt {
NSString *rateStr = [self stringLeastOneDecimals:flt];
NSArray *array = [rateStr componentsSeparatedByString:@"."];
NSString *pointStr = [array lastObject];
if (pointStr.length>4) {
return [NSString stringWithFormat:@"%.4f", flt];
}else {
return rateStr;
}
}
@end