【问题标题】:How to not show unnecessary zeros when given integers but still have float answers when needed如何在给定整数时不显示不必要的零,但在需要时仍然有浮点答案
【发布时间】:2013-10-29 19:57:45
【问题描述】:

我有一个正在开发的应用程序,我的一个功能是在需要时以浮点数或双精度值给出答案,当答案是整数时给出整数

例如,如果答案为 8.52,则答案变为 8.52,但当答案为 8 时,答案为 8 而不是 8.0000,我不希望它显示所有额外的 0。

- (IBAction) equalsbutton {
NSString *val = display.text;
switch(operation) {

    case Plus :
        display.text= [NSString stringWithFormat:@"%qi",[val longLongValue]+[storage longLongValue]];

    case Plus2 :
        display.text= [NSString stringWithFormat:@"%f",[val doubleValue]+[storage doubleValue]];

此代码似乎不起作用

【问题讨论】:

    标签: ios objective-c nsstring format-specifiers stringwithformat


    【解决方案1】:

    这些说明符是standard IEEE format specifiers,这意味着您可以执行%.2f 之类的操作,以仅在浮点变量上显示2 位小数。

    您也可以将其转换为int,然后使用%d 格式说明符,如果您想这样做的话。

    这里还有 Apple's documentation 的主题。

    编辑:根据您对另一篇帖子的评论,您似乎正在寻找 %g,这实际上会从浮点数中删除无关的 0。

    display.text= [NSString stringWithFormat:@"%g",[val doubleValue]+[storage doubleValue]];
    

    我在这里找到了答案:Use printf to format floats without decimal places if only trailing 0s

    【讨论】:

      【解决方案2】:

      编辑格式

      这是我在需要显示货币时执行此操作的一种方式(如果货币是整数,则为整数。

      首先我们以字符串形式获取金额

      NSString *earnString = _money.payout.displayableAmount;
      
      NSMutableString *strippedString = [NSMutableString
                                         stringWithCapacity:earnString.length];
      

      //扫描字符串以删除除数字以外的任何内容(包括小数点)

      NSScanner *scanner = [NSScanner scannerWithString:earnString];
      NSCharacterSet *numbers = [NSCharacterSet
                                 characterSetWithCharactersInString:@"0123456789"];
      
      while ([scanner isAtEnd] == NO) {
          NSString *buffer;
          if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
              [strippedString appendString:buffer];
      
          } else {
              [scanner setScanLocation:([scanner scanLocation] + 1)];
          }
      }
      //create an int with this new string
      int earnInt = [strippedString intValue];
      

      //如果字符串小于 100 那么我们只有“change”所以显示那个数量 如果(earnInt

      //如果我们有一个能被 100 整除的数字,那么我们有整个美元金额,正确显示 }else if(earnInt % 100 == 0){

          //The amount is exactly a dollar, display the whole number
          NSString *wholeDollar = [NSString stringWithFormat:@"$%i", (earnInt/100)];
          earnAmount.text = wholeDollar;
      

      //最后,如果我们有一个混合数字,那么将它们与中间的小数放在一起。

      }else{
          //Dollar amount is not exactly a dollar display the entire amount
          NSString *dollarString = [NSString stringWithFormat: @"$%0d.%02d", (earnInt / 100), (earnInt % 100)];
          earnAmount.text = dollarString;
      
      }
      

      希望对你有所帮助...

      【讨论】:

        【解决方案3】:

        你可以试试这个方法调用:

        [NSString stringWithFormat:@"%.0f", [val doubleValue] + [storage doubleValue]];
        

        【讨论】:

        • 我这样做了,但它似乎使它达到了 0 指定它可以使用的所有小数位数的位置。我使用 .1f,然后答案 8.555555 将变为 8.5 我只想允许答案 8.555555 但不允许答案像 8.500000 我不想在小数中显示额外的零
        【解决方案4】:

        最简单的方法,就是 NSNumberFormatter。如果需要,它只会显示小数点。示例(斯威夫特):

        let num1: Double = 5
        let num2: Double = 5.52
        let numberFormatter = NSNumberFormatter()
        numberFormatter.numberStyle = .DecimalStyle
        print(numberFormatter.stringFromNumber(NSNumber(double: num1)))
        print(numberFormatter.stringFromNumber(NSNumber(double: num2)))
        

        这将打印 5,然后是 5.52。

        【讨论】:

          猜你喜欢
          • 2012-05-10
          • 1970-01-01
          • 2013-06-02
          • 2013-07-27
          • 1970-01-01
          • 2019-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多