objective c - Specify floating point number accuracy in NSString -
i'm trying specify accuracy of floating pointer number in nsstring keep on getting error:
format specifies type 'double' argument has type 'nsstring *'
here code:
nsstring *tmplabel = [tmp description]; temp.text = [nsstring stringwithformat: @"%.fº", tmplabel];
what doing wrong?
your tmplabel nsstring
, should double
.
if sure tmplabel holds number can parsed simple double, can use doublevalue
on it. otherwise might have more elaborate conversion error handling etc.
here's down-to-earth simple way it:
temp.text = [nsstring stringwithformat: @"%.fº", [tmplabel doublevalue]];
the doublevalue
method (and close relatives integervalue
, , similar) interprets string's content , if beginning of string can read number, convert , return appropriate primitive type. in case primitive type double
.
you can not make type cast object (anything ns*) primitive type, have go through sort of interpretation or programmatic conversion.
last, please notice using doublevalue
directly on user input shortcut want re-visit @ point, make mental note of re-implementing using either number formatter or nsscanner
later. idea why, try enter non-numerical data , see if happy result.
finally, set accuracy (i.e., number of decimals after decimal point) asked for, see either prince's answer below, or dharaparekh's.
Comments
Post a Comment