【代码笔记】iOS-浮点数处理并去掉多余的0

一,代码。

复制代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSLog(@"--float-%@-",[self stringDisposeWithFloat:9.000899990000]);
    
}
#pragma -mark -functions
//浮点数处理并去掉多余的0
-(NSString *)stringDisposeWithFloat:(float)floatValue
{
    NSString *str = [NSString stringWithFormat:@"%f",floatValue];
    long len = str.length;
    for (int i = 0; i < len; i++)
    {
        if (![str  hasSuffix:@"0"])
            break;
        else
            str = [str substringToIndex:[str length]-1];
    }
    if ([str hasSuffix:@"."])//避免像2.0000这样的被解析成2.
    {
        //s.substring(0, len - i - 1);
        return [str substringToIndex:[str length]-1];
    }
    else
    {
        return str;
    }
}
复制代码

 

二,输出。

2015-10-16 09:53:45.765 浮点数处理并去掉多余的0[1554:47555] --float-9.0009-
2015-10-16 09:53:48.824 浮点数处理并去掉多余的0[1554:47619] Terminating since there is no system app.

 

原文地址:https://www.cnblogs.com/yang-guang-girl/p/5233788.html