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---  
  • 相关阅读:
    linux命令练习
    Centos修改用户密码
    Git使用
    使用SecureCRT连接虚拟机中Linux系统 和 虚拟机网络配置
    CentOS里Tomcat端口开放
    Linux下安装Tomcat
    laravel 中将DB::select 得到的内容转为数组
    nginx 转发 由于php语法错误 导致的 50x
    windows 下后台启动 redis
    sublime 非常好用的注释工具
  • 原文地址:https://www.cnblogs.com/xuejinhui/p/4552901.html
Copyright © 2011-2022 走看看