ios - How to use NSNumberFormatter to format numbers to a NSString of limited characters? -
For example, a calculator can only display up to 9 characters.
1/2 - & gt; 0.5 2/3 - & gt; 0.6666667 3/4 - & gt; 0.75 20000/3 - & gt; 6666.6667 123456x123456 - & gt; 1.5241E10 --------- 123456789
NSNumberFormatter . [numberFormatter setUsesSignificantDigits: Yes]; [Numeraphor setmxtimegupta dygets: 9]; [NumberFormatter set number number: NSNumberFormatterScientificStyle]; For the whole number integer with more than 9 digits, you can not do more than use the NSNumberFormatterScientificStyle . After checking this, a small program (see below) - you are still going to have problems with multi digit expans you can be better than multiple NSNumberFormatter s And check what is before displaying this number. (directly from the codecerner copy and paste)
#import & lt; Foundation / Foundation.h & gt; Static Zero Test (NSNumber * N) {NSNumberFormat * F = [NSNumberFormer New]; [F setus qualitative diets: yes]; [Fetmaximmugt Digits: 9]; [F setNumberStyle: NSNumberFormatterScientificStyle]; NSString * s = [f stringFromNumber: n]; NSLog (@ "% @ ->% @ (% lu)", N, S, [S's Length]); } Int main (int arguments, four * argv []) {@autoreleasepool {test (@ (1.0 / 2)); Test (@ (2.0 / 3)); Test (@ (3.0 / 4)); Test (@ (20000,0 / 2)); Test (@ 123456 * 123456); Test (@ (0.000000000001)); Test (@ (12345678.9000000000001)); Test (@ 12,345.9999999999999); }} Output 2014-04-24 12: 33: 01.480 untitled [3006: 507] 0.5 - & Gt; 5 E-1 (4) 2014-04-24 12:33 33: 01.481 untitled [3006: 507] 0.6666666666666666 - & gt; 6.66666667 E-1 (13) 2014-04-24 12: 33: 01.481 untitled [3006: 507] 0.75 - & gt; 7.5 E-1 (6) 2014-04-24 12:33 33: 01.482 Untitled [3006: 507] 10000 - & gt; 1 E4 (3) 2014-04-24 12:33 33: 01.482 Untitled [3006: 507] -1938485248 - & gt; -1.93848525 A9 (13) 2014-04-24 12:33 33: 01.483 Untitled [3006: 507] 1e-12 - & gt; 1 E-12 (5) 2014-04-24 12:33 33: 01.483 Untitled [3006: 507] 123456789 - & gt; 1.23456789 E8 (12) 2014-04-24 12:33 33: 01.484 Untitled [3006: 507] 12346 - & gt; 1.2346 E4 (8)
Comments
Post a Comment