Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何转换字符串"$双“U号”;加倍_Ios_Objective C - Fatal编程技术网

Ios 如何转换字符串"$双“U号”;加倍

Ios 如何转换字符串"$双“U号”;加倍,ios,objective-c,Ios,Objective C,我是目标c的新手,我有很多格式为“$99.99”(“$double_number”)的字符串。 如何汇总所有价格并获得一个实数?一个快速解决方案是删除$并找到doubleValue,如下所示: NSString *str = @"$99.99"; str = [str substringFromIndex:1]; double price = [str doubleValue]; NSLog(@"--> %.2f", price); //return 99.99 注意:double值具

我是目标c的新手,我有很多格式为“$99.99”(“$double_number”)的字符串。
如何汇总所有价格并获得一个实数?

一个快速解决方案是删除
$
并找到doubleValue,如下所示:

NSString *str = @"$99.99"; 
str = [str substringFromIndex:1];
double price = [str doubleValue];
NSLog(@"--> %.2f", price); //return 99.99

注意:
double
值具有更高的精度(十进制后的值),因为您只需要两个,我使用了
.2f

一个快速解决方案是删除
$
并查找double值,如下所示:

NSString *str = @"$99.99"; 
str = [str substringFromIndex:1];
double price = [str doubleValue];
NSLog(@"--> %.2f", price); //return 99.99
NSString *stringValue = @"$99.99";
NSString *price = [stringValue substringFromIndex:1];
double price = [price doubleValue];
NSLog(@"price : %.2f", price); 
注意:
double
值具有更高的精度(小数点后的值),因为您只需要两个,我使用了
.2f

NSString *stringValue = @"$99.99";
NSString *price = [stringValue substringFromIndex:1];
double price = [price doubleValue];
NSLog(@"price : %.2f", price); 
说明:

首先删除美元符号,然后使用doubleValue将其转换为double

说明:


首先删除美元符号,然后使用doubleValue将其转换为double,然后使用
NSStrings

NSString *strWithDollar = @"$4.95";
if([strWithDollar hasPrefix:@"$"])
{
    strWithDollar = [strWithDollar substringFromIndex:1];
}
double price = [strWithDollar doubleValue];
NSLog(@"price : %.2f", price); //return 4.95

使用
NSStrings
hasPrefix

NSString *strWithDollar = @"$4.95";
if([strWithDollar hasPrefix:@"$"])
{
    strWithDollar = [strWithDollar substringFromIndex:1];
}
double price = [strWithDollar doubleValue];
NSLog(@"price : %.2f", price); //return 4.95

您想要整数还是双精度/浮点?@Anoop Vaidya我想要双精度,从字符串中提取该值(例如“$99.99”得到99.99)您想要整数还是双精度/浮点?@Anoop Vaidya我想要双精度,从字符串中提取该值(例如“$99.99”得到99.99)我真的无法与如此简单的+1争论只有一次我想用
NSNumberFormatter
来实现复杂而实时的货币转换。我真的无法与如此简单的+1争论只有一次我想用
NSNumberFormatter
来实现复杂而实时的货币转换。你可以编辑答案以满足他的要求通过输入
.2f
来满足要求,因为他最多需要两位小数。您可以通过输入
.2f
来编辑答案以满足他的要求,因为他最多需要两位小数。