简体   繁体   English

在Objective C中将字符串转换为float而不进行舍入。

[英]Convert a string to float in Objective C without rounding it.?

How can i convert a string to float in Objective C without rounding it.? 如何在不对其进行舍入的情况下将字符串转换为在Objective C中浮动。

I tried to convert a string 8.56021285234; 我试着转换字符串8.56021285234;

float result=[string floatValue];

giving 8.560213, the rounded value. 给出8.560213,舍入值。

How can i avoid this? 我怎么能避免这个? How i can get the exact value? 我怎么能得到确切的价值?

You can't put more than about 7 digits of precision in a float. 你不能在浮点数中输入超过7位数的精度。 You need a type with more precision, like double, see here: 你需要一个更精确的类型,比如double,请看这里:

Difference between float and double float和double之间的区别

The problem is you're using a float. 问题是你正在使用浮动。 A float can usually only hold around 7-digits max. 浮动通常最多只能容纳7位数。 A double can hold many more digits. 双人可以容纳更多数字。 A float is 32-bit while a double is 64-bit therefore giving it "double" the precision. 浮点数为32位,而double为64位,因此精度为“double”。 A simple work-around to your problem would be to do: 解决问题的一个简单方法是:

double result = [string doubleValue];

When logging, make sure to use NSLog(@"%.12f",result); 记录时,请务必使用NSLog(@"%.12f",result); to show the entire double as %f defaults to only a 6 decimal precision. 显示整个双精度%%f默认只有6位小数精度。

Try double instead of float: 尝试double而不是float:

NSString *val = @"8.56021285234";
double num = [val doubleValue];
NSLog(@"Number ==> %f",num);

If you want to convert it use 如果你想转换它使用

 double result = [string doubleValue];

For displaying it use %.10f to specify decimal places: 要显示它,请使用%.10f指定小数位:

NSLog(@"%.10f", result);
 NSString *value = @"8.44654656565";
double dblValue = [value doubleValue];
NSLog(@"%.10f",dblValue);

the double variable have the all information you need, then you format it as text system can display not the whole digits stored in double variable double变量具有您需要的所有信息,然后您将其格式化为文本系统可以显示不存储在double变量中的整个数字

The Sabareesh code: Sabareesh代码:

NSString *value = @"8.44654656565";
double dblValue = [value doubleValue];
NSLog(@"%.12f",dblValue);

here dblValue have in memory all digits you want and you can use it as you want 这里dblValue在内存中有你想要的所有数字,你可以根据需要使用它
NSLog here can prove you have enough data - simply because NSLog output it 这里的NSLog可以证明你有足够的数据 - 只是因为NSLog输出它

Please try NSDecimal Number instead of [string floatValue]; 请尝试NSDecimal Number而不是[string floatValue];

NSDecimalNumber *price1 = [NSDecimalNumber decimalNumberWithString:@"15.99"];
NSDecimalNumber *price2 = [NSDecimalNumber decimalNumberWithString:@"29.99"];
NSDecimalNumber *coupon = [NSDecimalNumber decimalNumberWithString:@"5.00"];
NSDecimalNumber *discount = [NSDecimalNumber decimalNumberWithString:@".90"];
NSDecimalNumber *numProducts = [NSDecimalNumber decimalNumberWithString:@"2.0"];

NSDecimalNumber *subtotal = [price1 decimalNumberByAdding:price2];
NSDecimalNumber *afterCoupon = [subtotal decimalNumberBySubtracting:coupon];
NSDecimalNumber *afterDiscount = [afterCoupon decimalNumberByMultiplyingBy:discount];
NSDecimalNumber *average = [afterDiscount decimalNumberByDividingBy:numProducts];
NSDecimalNumber *averageSquared = [average decimalNumberByRaisingToPower:2];

NSLog(@"Subtotal: %@", subtotal);                    // 45.98
NSLog(@"After coupon: %@", afterCoupon);           // 40.98
NSLog((@"After discount: %@"), afterDiscount);       // 36.882
NSLog(@"Average price per product: %@", average);    // 18.441
NSLog(@"Average price squared: %@", averageSquared); // 340.070481

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM