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---  
  • 相关阅读:
    %u编码
    总结
    windows7 安装PHP7 本地网站搭建
    统计某个端口的链接数
    mysql连结查询
    mysql in
    读书笔记<白帽子讲web安全>
    Web攻防系列教程之文件上传攻防解析(转载)
    攻防:文件上传漏洞的攻击与防御
    weblogic检查项
  • 原文地址:https://www.cnblogs.com/xuejinhui/p/4552901.html
Copyright © 2011-2022 走看看