intValue 缺少小数位

发布于 2024-08-04 05:37:57 字数 741 浏览 7 评论 0原文

我有一个需要根据所选货币进行转换的价格。

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 是一个 NSNumbervalueForKey 也是我设置了转换率的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自由如风 2024-08-11 05:37:57

int 是一种整数类型 - 根据定义,它没有十进制值。相反尝试:

float fprice = [price floatValue];
float currencyValue = [[plistDictionary valueForKey:@"EUR"] floatValue];
float convertedCurrency = (fprice / currencyValue);

int is an integer type - by definition it does not have a decimal value. Instead try:

float fprice = [price floatValue];
float currencyValue = [[plistDictionary valueForKey:@"EUR"] floatValue];
float convertedCurrency = (fprice / currencyValue);
攒一口袋星星 2024-08-11 05:37:57

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.

妥活 2024-08-11 05:37:57

获取价格字符串并删除句点。之后将 NSString 转换为 int,这意味着您最终得到 4235 便士(或 42 美元 35 美分)。 (另外,请确保您获得的价格字符串有两位小数!有些人很懒,将“$3.30”输出为“3.3”。)

NSString *removePeriod = [price stringByReplacingOccurrencesOfString:@"." withString:@""];
int convertedPrice = [removePeriod intValue];
float exchangeRate;

然后根据选择的货币获取汇率并使用以下内容代码:

int convertedCurrency = round((double)convertedPrice / exchangeRate);
addCurrency = [NSString stringWithFormat: @"%0d.%02d", (convertedCurrency / 100), (convertedCurrency % 100)];

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".)

NSString *removePeriod = [price stringByReplacingOccurrencesOfString:@"." withString:@""];
int convertedPrice = [removePeriod intValue];
float exchangeRate;

Then get the exchange rates depending on which currency has been selected and use the following code:

int convertedCurrency = round((double)convertedPrice / exchangeRate);
addCurrency = [NSString stringWithFormat: @"%0d.%02d", (convertedCurrency / 100), (convertedCurrency % 100)];

addCurrency is your final price.

倥絔 2024-08-11 05:37:57

为了处理精确的十进制数,如果您的所有货币都有 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文