[英]Objective C : Get correct float values(justified)
I worked a lot in it and can't find a solution. 我做了很多工作,却找不到解决方案。 Even the title can't explain clearly. 甚至标题也无法清楚解释。
I have three values weight, quantity and total 我有三个值,重量,数量和总计
I had done the following 我做了以下
float wq = [[weightarray objectAtIndex:selectedint]floatValue];
float q = [quantity floatValue];
float total = wq * q;
for ex, if 例如,如果
[weightarray objectAtIndex:selectedint] = @"3.14";
quantity = 4;
then the result is 那么结果是
wq = 3.140000 q= 4.000000 total = 12.560000
but I need 但是我需要
wq = 3.14 total = 12.56
what to do? 该怎么办? I searched a lot, someone suggests to use NSDecimal, 我搜索了很多,有人建议使用NSDecimal,
NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:FALSE raiseOnOverflow:TRUE raiseOnUnderflow:TRUE raiseOnDivideByZero:TRUE];
but the scale is not 2 here, wq value may have 3 or four numbers after point. 但此处的小数位数不是2,因此wq值在点后可能有3或4个数字。
If the total = 2.30000100 means I need total = 2.300001 how to solve this? 如果总数= 2.30000100表示我需要总数= 2.300001怎么解决?
I'm not entirely sure what it is your asking for, but it seems as if you want the values to only display a 2 dp In which case you could use a string format like so: 我不确定您要的是什么,但似乎您只希望这些值仅显示2 dp,在这种情况下,您可以使用如下字符串格式:
NSString *output = [NSString stringWithFormat:@"float = %.2f", 3.14];
The .2 specifies that the float should be justified to 2 dp .2指定浮点数应调整为2 dp
Hope this helps 希望这可以帮助
There may be a more direct way to achieve it (which I don't know) but here's a suggestion... 可能有更直接的方法来实现它(我不知道),但这是一个建议...
[myString hasSuffix:@"0"]
to see if it ends in zero. 使用[myString hasSuffix:@"0"]
查看它是否以零结尾。 [myString substringToindex:[myString length]-1]
to create a new string without the final zero. 使用[myString substringToindex:[myString length]-1]
创建一个没有最后零的新字符串。 I know it's not elegant, but unless someone has a better solution, this will at least do what you want. 我知道这并不优雅,但是除非有人有更好的解决方案,否则这至少会做您想要的。
UPDATE: scratch that - I just discovered [myString stringByTrimmingCharactersInSet:set]
. 更新:从头开始-我刚刚发现了[myString stringByTrimmingCharactersInSet:set]
。 Surely this must be what you need...? 当然这必须是您所需要的...?
Finally solution found, thanks to Martin 终于找到了解决方案,多亏了马丁
float total = 12.56000;
NSString *s = [NSString stringWithFormat:@"%f", total];
NSLog(@"%@",s);
BOOL success;
success =NO;
while(!success)
{
if ([s hasSuffix:@"0"])
{
s = [s substringWithRange:NSMakeRange(0,[s length]-1)];
}
else if ([s hasSuffix:@"."])
{
s = [s substringWithRange:NSMakeRange(0,[s length]-1)];
success = YES;
}
else
success = YES;
}
NSLog(@"%@",s);
if total = 12.560000 it returns total = 12.56 如果total = 12.560000,则返回total = 12.56
if total = 12.000000 it returns total = 12 如果total = 12.000000,则返回total = 12
if total = 10.000000 it returns total = 10 如果total = 10.000000,则返回total = 10
if total = 12.3000100 it returns total = 12.30001 如果total = 12.3000100,则返回total = 12.30001
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.