Http://blog.sina.com.cn/s/blog_71715bf801017nyw.html
Method One:
-(NSString *) notrounding: (float) Price afterpoint: (int) position{
nsdecimalnumberhandler* Roundingbehavior = [Nsdecimalnumberhandler decimalnumberhandlerwithroundingmode: Nsrounddown scale:position raiseonexactness:no raiseonoverflow:no raiseonunderflow:no RaiseOnDivideByZero:NO];
Nsdecimalnumber *ouncesdecimal;
Nsdecimalnumber *roundedounces;
Ouncesdecimal = [[Nsdecimalnumber alloc] initwithfloat:price];
Roundedounces = [Ouncesdecimal decimalnumberbyroundingaccordingtobehavior:roundingbehavior];
[Ouncesdecimal release];
return [NSString stringwithformat:@ "%@", roundedounces];
}
Explain the parameters:
Price: Numbers that need to be processed,
Position: Keep the decimal point,
and then call
float s = 0.126;
NSString *SV = [self notrounding:s afterpoint:2];
NSLog (@ "SV =%@", SV);
The output is: SV = 0.12
The following describes the key parameters of Nsdecimalnumberhandler initialization: Decimalnumberhandlerwithroundingmode:nsrounddown,
The Nsrounddown represents is only to be placed.
The parameter position of scale represents a few digits after the decimal point is retained.
What do I do if I just get into it, for example, float 0.162 want 0.17? , there is a table on the development document that is treated as a reserved decimal point. I believe that we can see the following:
Method Two:
1, round (12345.6789) results are: 12346
2, round (12345.6789*100)/100 Results: 12345.68
The second is the result I want, but I do not understand this simple rounding to make so complicated, there should be better, I remember in other languages with: round (12345.6789,2) can be rounded to two decimal places.
----Rounding issues for iOS development