1.NSByteCountFormatter calculates the size of (formatted) files
We often need to calculate the file size. I see a lot of people using fucking formulas. Okay! In fact, Apple officially provides a compelling API, which converts the *** file byte number *** into the corresponding file size. Note that the *** file byte number * **, the type is long, not longer than that, if you exceed that, you may get a negative value.
NSByteCountFormatter *format = [[NSByteCountFormatter alloc] init];
format.allowedUnits = NSByteCountFormatterUseMB; //Output in MB
format.countStyle = NSByteCountFormatterCountStyleBinary;//1024 bytes are 1KB
format.includesUnit = YES; /// Output Result Display Unit
format.includesCount = YES; /// Output results display data
NSString *str = [format stringFromByteCount:4577781312988388823];
NSLog(@" %@ ",str);
2.NSDateFormatter Gets Weeks by Date
I've seen a lot of code written by many people, whether OC or Java calculates the week by date, using the same kind of calendar to get the day of the week, then NSDateFormatter actually provides a way to format the week, but also includes internationalization, that is, to set up the language environment.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ja"];
// zh output: Tuesday
// us output: Tuesday
// fr output: mardi
// ru output:
// th output: staggering
// ko output:
// ja output: day of fire
[format setDateFormat:@"EEEE"];
NSString *str = [format stringFromDate:[NSDate date]];
NSLog(@" %@ ",str);
[format setDateFormat:@"EEE"];
str = [format stringFromDate:[NSDate date]];
NSLog(@" %@ ",str);
This is objective-c code. It's similar to this in Java. The same is true for Java code.
SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE",Locale.GERMAN);
String string = dateFormat.format(new Date());
System.out.println(string);
3.NSDateIntervalFormatter Formatting for Two Date Ranges
This is used for two dates, the start date to the end date; then format the two dates
NSDateIntervalFormatter* formatter = [[NSDateIntervalFormatter alloc] init];
formatter.dateStyle = NSDateIntervalFormatterFullStyle;
formatter.timeStyle = NSDateIntervalFormatterFullStyle;
NSDate* startDate = [NSDate date];
NSDate* endDate = [NSDate dateWithTimeInterval:86400 sinceDate:startDate];
NSString* outputString = [formatter stringFromDate:startDate toDate:endDate];
NSLog(@" %@ ",outputString);
XX, XX, 20XX, XX, Tuesday, XX, XX, 20XX, XX, XX, XX, XX, XX, Wednesday, XX, 20XX, China Standard Time, XX:XX, afternoon
4. NSISO8601 DateFormatter Format Date
My understanding is to format the date or time quickly
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
formatter.formatOptions = NSISO8601DateFormatWithInternetDateTime;
NSString* outputString = [formatter stringFromDate:[NSDate date]];
NSLog(@"#### %@ ####",outputString);
Formatting output date or time can be understood as fast formatting date
5. NSMass Formatter Quality Formatting
Code directly
NSMassFormatter *formatter = [[NSMassFormatter alloc] init];
// [formatter.numberFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh"]];
formatter.unitStyle = NSFormattingUnitStyleLong;
NSString *outputString1 = [formatter stringFromValue:200 unit:NSMassFormatterUnitGram];
/// Output 200 grams
NSString *outputString2 = [formatter stringFromKilograms:200];
/// Output 200 kg
NSString *outputString3 = [formatter unitStringFromValue:33333.2 unit:NSMassFormatterUnitKilogram];
/// output kilograms
NSMassFormatterUnit uko = NSMassFormatterUnitKilogram;
NSString *outputString4 = [formatter unitStringFromKilograms:200 usedUnit:&uko];
/// output kilograms
6. NSLength Formatter Length Formatter Format
Format length, code directly
NSLengthFormatter *formatter = [[NSLengthFormatter alloc] init];
formatter.unitStyle = NSFormattingUnitStyleLong;
NSString *outputString1 = [formatter stringFromValue:200 unit:NSLengthFormatterUnitMeter];
/// Output 200 grams
NSString *outputString2 = [formatter stringFromMeters:222];
/// Output 200 kg
NSString *outputString3 = [formatter unitStringFromValue:33333.2 unit:NSLengthFormatterUnitMeter];
/// output kilograms
NSLengthFormatterUnit uko = NSLengthFormatterUnitMeter;
NSString *outputString4 = [formatter unitStringFromMeters:222 usedUnit:&uko];
/// output kilograms
7.NSEnergyFormatter Energy Formatting
In Joule
NSEnergyFormatter *formatter = [[NSEnergyFormatter alloc] init];
formatter.forFoodEnergyUse = YES;
double value = 1000.0;
NSString *outputString = [formatter stringFromJoules:value];
8.NSMeasurementFormatter Measurer
I will write an article with Dimension in the future. I'm looking forward to it!
9. Formatting of NSPersonName Components Formatter Business Card
Here I can't help but black three fat men, who let him grow fat
NSPersonNameComponentsFormatter *formatter = [[NSPersonNameComponentsFormatter alloc] init];
NSPersonNameComponents *p = [[NSPersonNameComponents alloc] init];
p.givenName = @"Three fat";
p.familyName = @"gold";
p.nameSuffix = @"Sir";
p.namePrefix = @"North Korean leaders";
p.nickname = @"Troisgros";
p.middleName = @"#";
NSString *outputString1 =[formatter stringFromPersonNameComponents:p];
// Kim Sanfat
formatter.style = NSPersonNameComponentsFormatterStyleLong;
NSString *outputString2 =[formatter stringFromPersonNameComponents:p];
//North Korean leader Kim Sang-fat # Sir
9.NSNumberFormatter digital formatting is more important
Digital formatting can be set up in different countries.
NSNumber *num1 = [NSNumber numberWithDouble:1234567.8369];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
// ==================== Class method====================
// Rounded integers
NSString *numberNoStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterNoStyle];
// decimal form
NSString *numberDecimalStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterDecimalStyle];
// Currency Form--Localization
NSString *numberCurrencyStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterCurrencyStyle];
// Percentage form
NSString *numberPercentStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterPercentStyle];
// Scientific Counting
NSString *numberScientificStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterScientificStyle];
// Reading aloud--localization
NSString *numberSpellOutStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterSpellOutStyle];
// Ordinal Form--Localization
NSString *numberOrdinalStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterOrdinalStyle];
// Currency Form ISO--Localization
NSString *numberCurrencyISOStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterCurrencyISOCodeStyle];
// Currency Form--Localization
NSString *numberCurrencyPluralStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterCurrencyPluralStyle];
// Accounting Counting--Localization
NSString *numberCurrencyAccountingStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterCurrencyAccountingStyle];
NSLog(@"No Style= %@",numberNoStyleStr); // No Style = 1234568
NSLog(@"Decimal Style= %@",numberDecimalStyleStr); // Decimal Style = 1,234,567.837
NSLog(@"Currency Style = %@",numberCurrencyStyleStr); // Currency Style = $1,234,567.84
NSLog(@"Percent Style = %@",numberPercentStyleStr); // Percent Style = 123,456,784%
NSLog(@"Scientific Style = %@",numberScientificStyleStr); // Scientific Style = 1.2345678369E6
// Spell Out Style = one million two hundred thirty-four thousand five hundred sixty-seven point eight three six nine
NSLog(@"Spell Out Style = %@",numberSpellOutStyleStr);
NSLog(@"Ordinal Style = %@",numberOrdinalStyleStr); // Ordinal Style = 1,234,568th
NSLog(@"Currency ISO Style = %@",numberCurrencyISOStyleStr); // Currency ISO Style = USD1,234,567.84
NSLog(@"Currency plural Style = %@",numberCurrencyPluralStyleStr); // Currency plural Style = 1,234,567.84 US dollars
NSLog(@"Currency accounting Style = %@",numberCurrencyAccountingStyleStr); // Currency accounting Style = $1,234,567.84
// ==================== Customization====================
NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
// Format width
numberFormatter.formatWidth = 15;
// Filler
numberFormatter.paddingCharacter = @"?";
// Filling position
numberFormatter.paddingPosition = kCFNumberFormatterPadBeforeSuffix;
numberFormatter.positiveSuffix = @"element";
NSLog(@"%@",[numberFormatter numberFromString:@"10000000 element"]); // 10000000
// Scaling factor, you can scale a number to a specified proportion, and then add a suffix to it, such as passing in a 3000, you want to represent 3000, you need to use this property.
// Prevent affecting later tests, so delete
// numberFormatter.multiplier = @1000;
// NSLog(@"%@1000", [numberFormatter numberFromString:@"1000");//1000
// numberFormatter.multiplier = @0.001;
// numberFormatter.positiveSuffix = 1000;
// NSLog (@"%@", [numberFormatter string FromNumber:@10000]); // 10000
// The mechanism is not clear, negative sign, positive sign
// numberFormatter.negativeFormat = @"^";
// numberFormatter.positiveFormat = @"~0";
// It seems useless.
numberFormatter.allowsFloats = NO;
numberFormatter.alwaysShowsDecimalSeparator = NO;
numberFormatter.maximum = @1000;
numberFormatter.minimum = @100;
// Decimal Point Style
numberFormatter.decimalSeparator = @".";
// Zero Style
numberFormatter.zeroSymbol = @"-";
// prefix and suffix
numberFormatter.positivePrefix = @"!";
numberFormatter.positiveSuffix = @"element";
numberFormatter.negativePrefix = @"@";
numberFormatter.negativeSuffix = @"Deficit";
// Specify symbols that are consistent with what we described in the previous class method
NSLog(@"Currency code%@",numberFormatter.currencyCode); // Currency code USD
NSLog(@"Monetary symbols%@",numberFormatter.currencySymbol); // Monetary symbols$
NSLog(@"International Monetary Symbols%@",numberFormatter.internationalCurrencySymbol); // International Monetary Symbol USD
NSLog(@"Percentage symbol%@",numberFormatter.percentSymbol); // Percentage symbol%
NSLog(@"Thousand-semicolon symbol%@",numberFormatter.perMillSymbol); // Thousand-semicolon symbol
NSLog(@"Minus sign%@",numberFormatter.minusSign); // Minus sign-
NSLog(@"Plus sign%@",numberFormatter.plusSign); // Plus sign+
NSLog(@"Exponential symbols%@",numberFormatter.exponentSymbol); // Exponential symbol E
// Maximum number of integers
numberFormatter.maximumIntegerDigits = 10;
// Minimum digits of integers
numberFormatter.minimumIntegerDigits = 2;
// Maximum decimal digits
numberFormatter.maximumFractionDigits = 3;
// Minimum decimal digits
numberFormatter.minimumFractionDigits = 1;
// Dimensions of Digital Segmentation
numberFormatter.groupingSize = 4;
// In addition to the size determined by grouping Size, the size of other digital bits
numberFormatter.secondaryGroupingSize = 2;
// Number of Maximum Significant Numbers
numberFormatter.maximumSignificantDigits = 12;
// Number of Minimum Significant Numbers
numberFormatter.minimumSignificantDigits = 3;
NSLog(@"Positive number%@,negative%@",[numberFormatter stringFromNumber:@(+12135230.2346)],[numberFormatter stringFromNumber:@(-12135231.2346)]); // Positive! 12,13,5230.2346 yuan, negative @12,13,5231.2346 deficit
NSLog(@"Zero = %@",[numberFormatter stringFromNumber:@(0)]); // Zero=-
// Rounding values, such as 10 carry values, then 156 carry 160, 154 carry 150
numberFormatter.roundingIncrement = @10;
// Rounding mode
numberFormatter.roundingMode = kCFNumberFormatterRoundHalfUp;
NSLog(@"%@",[numberFormatter stringFromNumber:@123456.7890]); // 12,3460 yuan