iOS float小数四舍五入

http://blog.csdn.net/fanjunxi1990/article/details/21536189

直接贴代码了

 

[python] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. #import "ViewController.h"  
  2.  
  3. @interface ViewController ()  
  4.  
  5. @end  
  6.  
  7. @implementation ViewController  
  8.   
  9. - (void)viewDidLoad  
  10. {  
  11.     [super viewDidLoad];  
  12.     NSLog(@"----%@---",[self decimalwithFormat:@"0.0000" floatV:0.334]);  
  13.     NSLog(@"----%@---",[self decimalwithFormat:@"0.0000" floatV:0.334569]);  
  14.     NSLog(@"----%@---",[self decimalwithFormat:@"0.0000" floatV:0.334519]);  
  15. }  
  16.   
  17. - (void)didReceiveMemoryWarning  
  18. {  
  19.     [super didReceiveMemoryWarning];  
  20.     // Dispose of any resources that can be recreated.  
  21. }  
  22.   
  23. //格式话小数 四舍五入类型  
  24. - (NSString *) decimalwithFormat:(NSString *)format  floatV:(float)floatV  
  25. {  
  26.     NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];  
  27.       
  28.     [numberFormatter setPositiveFormat:format];  
  29.       
  30.     return  [numberFormatter stringFromNumber:[NSNumber numberWithFloat:floatV]];  
  31. }  
  32.  
  33. @end  


最后控制台打印结果:

 

 

[python] view plaincopy在CODE上查看代码片派生到我的代码片
 
    1. 2014-03-19 15:44:45.262 TestNumberFormatter[3056:60b] ----0.3340---  
    2. 2014-03-19 15:44:45.263 TestNumberFormatter[3056:60b] ----0.3346---  
    3. 2014-03-19 15:44:45.263 TestNumberFormatter[3056:60b] ----0.3345---  

posted on 2015-06-04 20:28  沉淀2014  阅读(839)  评论(0编辑  收藏  举报

导航