c++ - Round decimal for a float number -
in vc++ have float number 1.32544354353 i.e float num=1.32544354353;
want first 1 digit after point. i.e 1.3 (not 1.300000000). how can this? please me... if solution in cocos2dx thats better(i want in cocos2d-x game)
the answer depends on type want result be.
if want obtain result float
, impossible. float
's point of view, 1.3 , 1.300000000 same values , can not have float
holds 1.3, not 1.300000000. can here drop digits using, e.g., (int)(val*10)/10.0
. (however, float number precision problems not make results 1.3, line 1.2999999982 or 1.3000000029 random digits @ end.)
if want string (meaning string in general) representation, can gen string "1.3"
. obtain this, use precision specifiers such "%.1f"
or std::setprecision(1)
.
Comments
Post a Comment