http://blog.csdn.net/fanjunxi1990/article/details/21536189
The code is directly attached.
[Python]View Plaincopy
- #import "ViewController.h"
- @interface Viewcontroller ()
- @end
- @implementation Viewcontroller
- -(void) viewdidload
- {
- [Super Viewdidload];
- NSLog (@"----%@---", [selfdecimalwithformat:@"0.0000" FLOATV:0.334]);
- NSLog (@"----%@---", [selfdecimalwithformat:@"0.0000" FLOATV:0.334569]);
- NSLog (@"----%@---", [selfdecimalwithformat:@"0.0000" FLOATV:0.334519]);
- }
- -(void) didreceivememorywarning
- {
- [Super didreceivememorywarning];
- Dispose of any resources the can be recreated.
- }
- Format session Decimal Rounding type
- -(NSString *) Decimalwithformat: (NSString *) format FLOATV: (float) FLOATV
- {
- Nsnumberformatter *numberformatter = [[Nsnumberformatter alloc] init];
- [Numberformatter Setpositiveformat:format];
- return [Numberformatter stringfromnumber:[nsnumber NUMBERWITHFLOAT:FLOATV]];
- }
- @end
Final Console Print Results:
[Python]View Plaincopy
- 2014-03-: 45.262 testnumberformatter[3056:60b]----0.3340---
- 2014-03-: 45.263 testnumberformatter[3056:60b]----0.3346---
- 2014-03-: 45.263 testnumberformatter[3056:60b]----0.3345---
IOS Float Decimal Rounding