How to trim zeros after decimal point

后端 未结 8 1669
栀梦
栀梦 2021-01-05 05:38

I am trying to trim zeros after a decimal point as below but it\'s not giving desired result.

trig = [currentVal doubleValue];
trig = trig/100;
NSNumberForma         


        
相关标签:
8条回答
  • 2021-01-05 06:06

    From the documentation, it looks like setFractionDigits: is only for converting the other way.

    The best thing to do is probably to convert your number to an integer before formatting it e.g.

     double converted = round(trig); // man round for docs
    

    You can use also the formatting functions of stringWithFormat: of NSString, but then you will lose all the localisation advantages you get with NSNumberFormatter.

    0 讨论(0)
  • 2021-01-05 06:06

    This may not be a proper solution where there is NSNumberFormetter Class, But I just did this rather then googling a lot! ;)

    Here is an example, if it helps:

    -(NSString*) trimZerosAfterDecimalPoint:(NSString*)string_ {
    
        double doubleValue=[string_ doubleValue];    
        long   leftPart=(long)doubleValue;
        double rightPart=doubleValue-(double)leftPart;
    
        NSString *rightPartAsStr=[NSString stringWithFormat:@"%f", rightPart];
    
        int i=0;
        for (i=rightPartAsStr.length-1; i>=2; i--) {
            if ([rightPartAsStr characterAtIndex:i]!='0') {            
                rightPartAsStr=[rightPartAsStr substringWithRange:NSMakeRange(2, i-1)];
                break;
            }
        }
    
        if (i<2) {
            string_=[NSString stringWithFormat:@"%ld", leftPart];
        } else {
            string_=[NSString stringWithFormat:@"%ld.%@", leftPart, rightPartAsStr];
        }
    
        return string_;
    }
    
    0 讨论(0)
  • 2021-01-05 06:08

    I just had to do this for one of my programs and heres how I went about it:

    - (void) simplify{
            int length = (int)[self.calcString length];
            for (int i = (int)[self.calcString length]; i > 0; i--) {
                if  ([self.calcString rangeOfString:@"."].location != NSNotFound) {
                    NSRange prevChar = NSMakeRange(i-1, 1);
                    if ([[self.calcString substringWithRange:prevChar] isEqualToString:@"0"]||
                        [[self.calcString substringWithRange:prevChar] isEqualToString:@"."]) 
                        length--;
                    else
                        break;
            }
            self.calcString = [self.calcString substringToIndex:length];
        }
    }
    
    0 讨论(0)
  • 2021-01-05 06:08

    This works

    display.text = [@(trig) stringValue];
    
    0 讨论(0)
  • 2021-01-05 06:09

    it is because of your datatype cannot be formatted is such a manner.

    0 讨论(0)
  • 2021-01-05 06:13

    In case this helps someone. I wanted 1 decimal value but no '.0' on the end if the float was '1.0'. Using %g would give scientific notation for longer numbers, following ugliness worked well enough for me as high accuracy wasn't critical.

    // Convert to 1 dp string,
    NSString* dirtyString = [NSString stringWithFormat: @"%.1f", self.myFloat];
    
    // Convert back to float that is now a maximum of 1 dp,
    float myDirtyFloat = [dirtyString floatValue];
    
    // Output the float subtracting the zeros the previous step attached
    return [NSString stringWithFormat: @"%g", myDirtyFloat];
    
    0 讨论(0)
提交回复
热议问题