NSDecimal 保留2位小数 NSDecimal 123.456 => NSDecimal 123.45 这是四舍五入么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 呵呵 粘错了NSDecimal 123.456 => NSDecimal 123.46 感谢邀请,下面是我自己用的保留有效小数位的方法(同时四舍五入):/** * @brief 截取指定小数位的值 * * @param price 目标数据 * @param position 有效小数位 * * @return 截取后数据 */+ (NSString *)notRounding:(float)price afterPoint:(NSInteger)position{ NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO]; NSDecimalNumber *ouncesDecimal; NSDecimalNumber *roundedOunces; ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price]; roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior]; [ouncesDecimal release]; return [NSString stringWithFormat:@"%@",roundedOunces];} 如果不想四舍五入直接截取的话就把 NSRoundPlain改为NSRoundDown即可。 谢谢。但我想要返回值是Decimal或DecimalNumber而不是string,难道要再转换一遍? 谢谢。但我想要返回值是Decimal或DecimalNumber而不是string,难道要再转换一遍?不用,把方法返回类型改为NSDecimalNumber,并 return [roundedOunces autorelease];就可以了,如果用ARC则无视release ios实现微博登录功能 如何屏蔽在工具条上的手势动作 MAC系统下的SCPlugin不能Checkout怎么解决?Server sent unexpected return value (403 Forbidden 关于苹果应用程序开发 如何在xcode中使用已经在gcc中编译好的库? iphone中如何获取磁盘的剩余空间大小 iphone开发技术要学习的内容 国内IOS,一般都开发些什么? IOS 自带facebook登录分享 IOS中View之间如何异步通信? oc中怎么调cpp文件里面的方法 用VMWARE装了MAC OS X 10.9后怎么才能连上网呢?主机是有线网。。。求解
NSDecimal 123.456 => NSDecimal 123.46
* @brief 截取指定小数位的值
*
* @param price 目标数据
* @param position 有效小数位
*
* @return 截取后数据
*/
+ (NSString *)notRounding:(float)price afterPoint:(NSInteger)position
{
NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber *ouncesDecimal;
NSDecimalNumber *roundedOunces;
ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];
roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
[ouncesDecimal release];
return [NSString stringWithFormat:@"%@",roundedOunces];
}
不用,把方法返回类型改为NSDecimalNumber,并 return [roundedOunces autorelease];就可以了,如果用ARC则无视release