zoukankan      html  css  js  c++  java
  • 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---  
  • 相关阅读:
    DYCom多平台聊天室Sample
    silverlight多人在线游戏示例
    设计模式之: 命令模式
    Vector
    flashBuilder加入AsDoc注释
    转:每个Flash开发者应该知道的事情
    flashBuilder无法调试:57%就不动了
    UML类图实例
    Dictionary
    Enterprise Architect使用(一)
  • 原文地址:https://www.cnblogs.com/xuejinhui/p/4552901.html
Copyright © 2011-2022 走看看