gpt4 book ai didi

iphone - 使用设备的货币设置格式化输出

转载 作者:行者123 更新时间:2023-11-28 23:16:18 25 4
gpt4 key购买 nike

我正在开发我的第一个 iPhone 应用程序,希望能够使用设备的货币设置来格式化货币。

目前我有这个:

- (IBAction)calculate {

float lp = ([leftPrice.text floatValue]);
float rp = ([rightPrice.text floatValue]);
float lw = ([leftWeight.text floatValue]);
float rw = ([rightWeight.text floatValue]);

float lppkg = lp/lw;
float rppkg = rp/rw;

if (lp > 0 && lw > 0) {
leftPricePerKg.text = [[NSString alloc] initWithFormat:@"$%.2f", lppkg];
}

if (rp > 0 && rw > 0) {
rightPricePerKg.text = [[NSString alloc] initWithFormat:@"$%.2f", rppkg];
}
}

最佳答案

你应该看看 NSNumberFormatter -

你可以看这里: https://developer.apple.com/documentation/foundation/numberformatter

在这里:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfNumberFormatting10_4.html#//apple_ref/doc/uid/TP40002368-SW1

特别是在

“配置货币格式”部分

编辑

我发现 -

//--------------------------------------------
// Format style as currency, output to console
//--------------------------------------------
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
formattedOutput = [formatter stringFromNumber:[attributesDict objectForKey:NSFileSystemFreeSize]];
NSLog(@"Output as currency: %@", formattedOutput);

//--------------------------------------------
// Change local and output as currency
//--------------------------------------------
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"it_IT"];
[formatter setLocale:locale];
formattedOutput = [formatter stringFromNumber:[attributesDict objectForKey:NSFileSystemFreeSize]];
NSLog(@"Output as currency - locale it_IT: %@", formattedOutput);

这里: http://iosdevelopertips.com/cocoa/formatting-numbers-nsnumberformatter-examples.html

关于iphone - 使用设备的货币设置格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6249437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com