将 NSDecimalNumber 转换为 NSString
我正在从如下所示的对象中检索键:
po obj
{
TypeID = 3;
TypeName = Asset;
}
键值的检索方式如下:
NSString *typeId = (NSString*)[obj objectForKey:@"TypeID"];
typeId 不是 NSString,而是 NSDecimalNumber。这是为什么?
如何将其转换为 NSString?
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 this:
NSString *typeId = (NSString*)[obj objectForKey:@"TypeID"];
Rather than typeId being an NSString, it is an NSDecimalNumber. Why is that?
How do I convert it to an NSString?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要使用
stringWithFormat
函数:或
stringValue
:You need to use the
stringWithFormat
function:or
stringValue
:这应该有效:
This should work:
如果你想获取字符串值,只需使用“description”即可。即使返回的类型是 NSDecimalNumber、NSString 或其他类型,它也能正常工作:
If you want to get string value, just use "description". It works fine even if returned type is NSDecimalNumber, or NSString, or whatever:
由于这是顶部搜索结果,并且我没有看到有关 Swift 中 NSDecimalNumber 到 String 的问题,所以就在这里。这将显示本地化的小数分隔符和所有具有非零值的小数位。
let string = DecimalNumber.description(withLocale: Locale.current)
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)
使用它来将 NSDecimalNumber 转换为 NSString:
使用它来将 NSString 转换为 NSDecimalNumber:
Use this to convert NSDecimalNumber to NSString:
Use this to convert NSString to NSDecimalNumber: