-1

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,

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.

If the total = 2.30000100 means I need total = 2.300001 how to solve this?

Nazik
  • 8,696
  • 27
  • 77
  • 123
  • To clarify - how do you want the figures truncated - to a known number of decimal places or to remove ending zeros - and if the aller how to dea with say 2.3 entered actually being stored as 2.300000001 ? – mmmmmm Feb 08 '13 at 10:43
  • I need to remove ending zeros only – Nazik Feb 08 '13 at 10:45
  • and you are aware that what you have in the string will not necessarily be what is in the float e.g. my 2.3 entered actually being stored as 2.300000001 ? – mmmmmm Feb 08 '13 at 10:46
  • if total = 2.0 means I need total = 2 only – Nazik Feb 08 '13 at 10:48
  • 2.3000000001 is not a problem, it can be as it is, but if there is 2.300000000100 means I need 2.3000000001 as result – Nazik Feb 08 '13 at 10:50

3 Answers3

3

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 d.p. In which case you could use a string format like so:

    NSString *output = [NSString stringWithFormat:@"float = %.2f", 3.14];

The .2 specifies that the float should be justified to 2 d.p.

Hope this helps

Tom Hancocks
  • 440
  • 2
  • 10
2

There may be a more direct way to achieve it (which I don't know) but here's a suggestion...

  1. Convert to string as you already do.
  2. Use [myString hasSuffix:@"0"] to see if it ends in zero.
  3. Use [myString substringToindex:[myString length]-1] to create a new string without the final zero.
  4. Repeat.

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]. Surely this must be what you need...?

Martin
  • 5,392
  • 30
  • 39
0

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

if total = 12.000000 it returns total = 12

if total = 10.000000 it returns total = 10

if total = 12.3000100 it returns total = 12.30001

Nazik
  • 8,696
  • 27
  • 77
  • 123