ios - adding total sum with floating points -
in xcode utilizing 4 utitextfields following chart user enter whole number, chart following:
total | win | place | show 34 | 4 | 5 | 3
my algorithm chart following:
-for total win * 1 -for total place * .5 -for total show * .25 -add 3 totals new float value (identified as, float totaltrackalltogether) -divide totaltrackalltogether total column (identified as, int tracktotalcolum) -display sum on onofflabel.text
here code portion not seem work , crashes
-(void)trackrecordmath { int trackwincolum = [trackwin.text intvalue]; int trackplacecolum = [trackplace.text floatvalue]; int trackshowcolum = [trackshow.text floatvalue]; int tracktotalcolum= [tracktotal.text intvalue]; int trackwintotal = trackwincolum * 1; nslog(@"%d", trackwintotal); int trackplacetotal = trackplacecolum *.5; nslog(@"%d", trackplacetotal); int trackshowtotal= trackshowcolum *.25; nslog(@"%d", trackshowtotal); float totaloftrackcolums = trackwintotal + trackplacetotal + trackshowtotal; float totaltrackalltogether= (float) totaloftrackcolums / tracktotalcolum; onofflabel.text = [[nsstring alloc] initwithformat:@"%f", totaltrackalltogether]; }
i seem held when trying multiply floating points whole numbers, , displaying through onofflabel.text. thank help!
these basic stuff works same in c, c++ , objective-c. if floating-point calculation , store result in int, after decimal point dropped.
furthermore, using float instead of double massive rounding errors without benefit. instead of
int trackplacecolum = [trackplace.text floatvalue];
write own of these:
nsinteger trackplacecolumn = [trackplace.text integervalue]; double trackplacecolumn = [trackplace.text doublevalue];
btw. it's column, not colum. , crash if divide zero. check whether tracktotalcolumn 0 or not in code.
Comments
Post a Comment