zoukankan      html  css  js  c++  java
  • iOS Round Double value to 2 decimal digits and round 3rd decimal digit up

    I have I double value like this one 17.125. It should be rounded up to 17.13

    If I use the simple method like %.2f it shows me 17.12 I also followed several methods described in other threads here like using NumberFormatter and so on - but without luck.

    Maybe someone has an advice for me to fix this problem? Do I have to round it on my own?

    Try This:

    float number=17.125;
    NSNumberFormatter *format = [[NSNumberFormatter alloc]init];
    [format setNumberStyle:NSNumberFormatterDecimalStyle];
    [format setRoundingMode:NSNumberFormatterRoundHalfUp];
    [format setMaximumFractionDigits:2];
    NSString *temp = [format stringFromNumber:[NSNumber numberWithFloat:number]];
    NSLog(@"%@",temp);

    http://stackoverflow.com/questions/15109963/ios-round-double-value-to-2-decimal-digits-and-round-3rd-decimal-digit-up

     
  • 相关阅读:
    状态模式
    适配器模式
    观察者模式
    浮点数在内存中的存储方式
    建造者模式
    外观模式
    模版方法模式
    原型模式
    Python 学习笔记6 变量-元组
    Python 学习笔记5 变量-列表
  • 原文地址:https://www.cnblogs.com/savagemorgan/p/4071552.html
Copyright © 2011-2022 走看看