如何获取NSDecimal或NSDecimalNumber的无格式字符串表示形式? [英] How to obtain an unformatted string representation of an NSDecimal or NSDecimalNumber?
问题描述
我有一个NSDecimal,需要这个技术字符串,即没有任何格式。浮点应该是一个。如果有任何,和减号应该只是一个 - 如果有任何。除此之外,应该没有类似分组或中文数字的格式。
I have an NSDecimal and need this as technical string, i.e. with no formatting in any way. Floating point should be a "." if there is any, and minus sign should just be a "-" if there is any. besides that, there should be no formatting going on like grouping or chinese numbers.
我通过SDK寻找2个小时,但似乎没有什么简单的。有没有解决方案?
I was looking for 2 hours through the SDK but it seems there is nothing simple to accomplish this. Are there solutions for this?
推荐答案
对于NSDecimal,你可以使用NSDecimalString指定的语言环境:
For NSDecimal, you can use NSDecimalString with a specified locale:
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *decimalString = NSDecimalString(&decimalValue, usLocale);
[usLocale release];
美国区域设置使用句点作为小数分隔符,没有千位分隔符,因此我相信
The U.S. locale uses a period for the decimal separator, and no thousands separator, so I believe that will get you the kind of formatting you're looking for.
正如其他人所指出的,对于NSDecimalNumber,您可以使用-descriptionWithLocale:方法与上述指定的US语言环境。这不会失去任何精确度。
As others have pointed out, for an NSDecimalNumber you can use the -descriptionWithLocale: method with the above-specified U.S. locale. This doesn't lose you any precision.
这篇关于如何获取NSDecimal或NSDecimalNumber的无格式字符串表示形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!