综合网上的各种不靠谱的算法,自己写的四舍五入方法
//单元测试通过/***四舍五入并保留N位小数**@paramnumber数值字符串*@paramafterPoint第几位小数**@return处理后结果*/+(NSString*)halfUpDecimalNumber:(NSString*)numberafterPoint:(int)position{/*Mode的枚举类型NSRoundPlain,//四舍五入NSRoundDown,//只舍不入NSRoundUp,//不舍只入NSRoundBankers//*/if([numberlength]==0){return@"";}NSDecimalNumber*ouncesDecimal=[NSDecimalNumberdecimalNumberWithString:number];NSString*formatStr=@"0.";for(inti=0;i<position;i++){formatStr=[formatStrstringByAppendingString:@"0"];}NSNumberFormatter*doubleValueWithMaxTwoDecimalPlaces=[[NSNumberFormatteralloc]init];[doubleValueWithMaxTwoDecimalPlacessetNumberStyle:NSNumberFormatterDecimalStyle];[doubleValueWithMaxTwoDecimalPlacessetPaddingCharacter:@""];[doubleValueWithMaxTwoDecimalPlacessetPaddingPosition:NSNumberFormatterPadAfterSuffix];[doubleValueWithMaxTwoDecimalPlacessetFormatWidth:position];[doubleValueWithMaxTwoDecimalPlacessetPerMillSymbol:@""];[doubleValueWithMaxTwoDecimalPlacessetAlwaysShowsDecimalSeparator:YES];[doubleValueWithMaxTwoDecimalPlacessetGroupingSeparator:@""];[doubleValueWithMaxTwoDecimalPlacessetRoundingMode:NSNumberFormatterRoundHalfUp];[doubleValueWithMaxTwoDecimalPlacessetPositiveFormat:formatStr];return[doubleValueWithMaxTwoDecimalPlacesstringFromNumber:ouncesDecimal];}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。