Converting NSDecimalNumber to NSString

后端 未结 5 1572
伪装坚强ぢ
伪装坚强ぢ 2021-02-02 15:06

I\'m retrieving a key from an object that looks like this:

po obj
{
  TypeID = 3;
  TypeName = Asset;
}

The key value is being retrieved like t

相关标签:
5条回答
  • 2021-02-02 15:25

    Use this to convert NSDecimalNumber to NSString:

    NSDecimalNumber* dec1;
    NSString* str;
    
    str = dec1.stringValue;
    

    Use this to convert NSString to NSDecimalNumber:

    NSString* string1 = @"Stack overflow 123";
    NSDecimalNumber* dec;
    
    dec = (NSDecimalNumber *)string1;
    
    0 讨论(0)
  • 2021-02-02 15:27

    You need to use the stringWithFormat function:

    NSString *typeId = [NSString stringWithFormat:@"%@", [obj objectForKey:@"TypeID"]];
    

    or stringValue:

    NSString *typeId = [[obj objectForKey:@"TypeID"] stringValue];
    
    0 讨论(0)
  • 2021-02-02 15:37

    This should work:

    NSString *typeId = [[obj objectForKey:@"TypeID"] stringValue];
    
    0 讨论(0)
  • 2021-02-02 15:46

    Since this is the top search result and I don't see a question for NSDecimalNumber to String in Swift, here it is. This will show the localized decimal separator and all decimal places with a non-zero value.

    let string = decimalNumber.description(withLocale: Locale.current)

    0 讨论(0)
  • 2021-02-02 15:48

    If you want to get string value, just use "description". It works fine even if returned type is NSDecimalNumber, or NSString, or whatever:

    NSString *typeId = [[obj objectForKey:@"TypeID"] description];
    
    0 讨论(0)
提交回复
热议问题