In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the method of formatting and displaying the amount string in iOS". In the daily operation, I believe that many people have doubts about the method of formatting and displaying the amount string in iOS. The editor consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful for you to answer the doubt of "what is the method of formatting and displaying the amount string in iOS?" Next, please follow the editor to study!
Preface
Because the statistical amount is shown in many places in the project, such as in some financial App, the string representing the amount class usually needs to be formatted and then displayed. For example:
0-> 0.00123-> 123.00123.456-> 123.46102000-> 102000.0010204500-> 10204500.00
Its rules are as follows:
Add a comma every three digits from single digits, while retaining two decimal places, also known as the "thousand format".
We started with a rather clumsy approach as follows:
First of all, according to the decimal point. Split the incoming string into two parts, the integer part and the decimal part (fill in .00 if there is no decimal point, and the amount is malformed if there is more than one decimal point). For the decimal part, take only the first two digits; then traverse the integer part of the string, inserting a comma before every three digits from right to left, and finally concatenate the two parts, the code is roughly as follows:
-(NSString *) moneyFormat: (NSString *) money {if (! money | | money.length = = 0) {return money;} BOOL hasPoint = NO; if ([money rangeOfString:@ "."] .length > 0) {hasPoint = YES;} NSMutableString * pointMoney = [NSMutableString stringWithString:money]; if (hasPoint = NO) {[pointMoney appendString:@ ".00"];} NSArray * moneys = [pointMoney componentsSeparatedByString:@ "."]; if (moneys.count > 2) {return pointMoney } else if (moneys.count = = 1) {return [NSString stringWithFormat:@ "% @ .00", moneys [0];} else {/ / Integer part inserts a comma NSString * frontMoney = [self stringFormatToThreeBit:moneys [0]] every 3 digits; if ([frontMoney isEqualToString:@ ""]) {frontMoney = @ "0";} / / concatenate integer and decimal parts NSString * backMoney = moneys [1] If ([backMoney length] = = 1) {return [NSString stringWithFormat:@ "% @.% @ 0", frontMoney, backMoney];} else if ([backMoney length] > 2) {return [NSString stringWithFormat:@ "% @.% @", frontMoney, [backMoney substringToIndex:2]];} else {return [NSString stringWithFormat:@ "% @.% @", frontMoney, backMoney];}
The implementation of the stringFormatToThreeBit: method is as follows:
-(NSString *) stringFormatToThreeBit: (NSString *) string {NSString * tempString = [string stringByReplacingOccurrencesOfString:@ "," withString:@ "]; NSMutableString * mutableString = [NSMutableString stringWithString:tempString]; NSInteger n = 2; for (NSInteger I = tempString.length-3; I > 0; iMuk -) {nails; if (n = 3) {[mutableString insertString:@", "atIndex:i]; n = 0;}} return mutableString;}
The above implementation looks very tedious.
In fact, Apple provides NSNumberFormatter to handle the conversion between NSString and NSNumber, which can meet the basic digital formatting.
Correct posture
We can achieve the above functions by setting the numberStyle and positiveFormat attributes of NSNumberFormatter, which is very concise. The code is as follows:
-(NSString *) formatDecimalNumber: (NSString *) string {if (! string | | string.length = = 0) {return string;} NSNumber * number = @ ([string doubleValue]); NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init]; formatter.numberStyle = kCFNumberFormatterDecimalStyle; formatter.positiveFormat = @ "#, # # 0.00"; NSString * amountString = [formatter stringFromNumber:number]; return amountString;}
At this point, the study on "what is the method of formatting and displaying the amount string in iOS" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.