IOS Float Decimal Rounding

Source: Internet
Author: User

http://blog.csdn.net/fanjunxi1990/article/details/21536189

The code is directly attached.

[Python]View Plaincopy
  1. #import "ViewController.h"
  2. @interface Viewcontroller ()
  3. @end
  4. @implementation Viewcontroller
  5. -(void) viewdidload
  6. {
  7. [Super Viewdidload];
  8. NSLog (@"----%@---", [selfdecimalwithformat:@"0.0000" FLOATV:0.334]);
  9. NSLog (@"----%@---", [selfdecimalwithformat:@"0.0000" FLOATV:0.334569]);
  10. NSLog (@"----%@---", [selfdecimalwithformat:@"0.0000" FLOATV:0.334519]);
  11. }
  12. -(void) didreceivememorywarning
  13. {
  14. [Super didreceivememorywarning];
  15. Dispose of any resources the can be recreated.
  16. }
  17. Format session Decimal Rounding type
  18. -(NSString *) Decimalwithformat: (NSString *) format FLOATV: (float) FLOATV
  19. {
  20. Nsnumberformatter *numberformatter = [[Nsnumberformatter alloc] init];
  21. [Numberformatter Setpositiveformat:format];
  22. return [Numberformatter stringfromnumber:[nsnumber NUMBERWITHFLOAT:FLOATV]];
  23. }
  24. @end


Final Console Print Results:

[Python]View Plaincopy
    1. 2014-03-: 45.262 testnumberformatter[3056:60b]----0.3340---
    2. 2014-03-: 45.263 testnumberformatter[3056:60b]----0.3346---
    3. 2014-03-: 45.263 testnumberformatter[3056:60b]----0.3345---

IOS Float Decimal Rounding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.