intValue 缺少小数位
我有一个需要根据所选货币进行转换的价格。
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"currency.plist"];
NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
int price = [price intValue];
int currencyValue = [[plistDictionary valueForKey:@"EUR"] intValue];
int convertedCurrency = (price / currencyValue);
price
是一个 NSNumber
,valueForKey
也是我设置了转换率的 plist 文件中的一个数字。
我遇到的问题是我的 price
缺少小数点。每次我从价格中获取 intValue 时,它都会向上或向下舍入。我从 plist 获得的汇率也存在同样的问题。
我已经研究过 NSNumberFormatter
但它不允许我 setFormat
NSNumberFormatter。请问有什么建议吗?
I have a price that I need to convert based on the selected currency.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"currency.plist"];
NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
int price = [price intValue];
int currencyValue = [[plistDictionary valueForKey:@"EUR"] intValue];
int convertedCurrency = (price / currencyValue);
price
is an NSNumber
and the valueForKey
is also a number from a plist file I have setup with conversion rates.
The problem I am having, is that my price
is missing the decimals. Everytime I get the intValue from the price it's just rounded up or down. The same issue exists for the exchange rate I get from the plist.
I have looked into NSNumberFormatter
but it won't let me setFormat
for the NSNumberFormatter. Any advice, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
int
是一种整数类型 - 根据定义,它没有十进制值。相反尝试:int
is an integer type - by definition it does not have a decimal value. Instead try:intValue 返回一个整数,该整数(根据定义)四舍五入为不带小数的数字。
您可以使用doubleValue,它返回一个double(它有小数部分)或decimalValue,它返回一个NSDecimal对象。
intValue returns an integer, which (by definition) is rounded to a number without decimals.
You could use doubleValue, which returns a double (which does have the fractional portion) or decimalValue which returns a NSDecimal object.
获取价格字符串并删除句点。之后将 NSString 转换为 int,这意味着您最终得到 4235 便士(或 42 美元 35 美分)。 (另外,请确保您获得的价格字符串有两位小数!有些人很懒,将“$3.30”输出为“3.3”。)
然后根据选择的货币获取汇率并使用以下内容代码:
addCurrency 是您的最终价格。
Take the price string and remove the period. After that convert the NSString to and int, which means you end up with 4235 pennies (or 42 dollars and 35 cents). (Also, make sure that the price string you're getting has two decimal places! Some people are lazy, and output "3.3" for "$3.30".)
Then get the exchange rates depending on which currency has been selected and use the following code:
addCurrency is your final price.
为了处理精确的十进制数,如果您的所有货币都有 2 位小数(例如不是日元),请将所有数字设为美分数
例如,将 43.35 欧元存储为 4235。
然后您可以在算术中使用,然后只需使用 value/100.0 和 NSNumberFormatter 处理格式
To deal with the exact number of deciamlas and if all your currencies have 2 decimal places (e.g not JPY) make all numbers the number of cents
e.g. store 43.35 EUR as 4235.
Then you can use in arithmetic and then just deal with formatting using value/100.0 and NSNumberFormatter