IOS returns a numeric string with no digits before the decimal point and two digits after the decimal point. ios decimal point

Source: Internet
Author: User

IOS returns a numeric string with no digits before the decimal point and two digits after the decimal point. ios decimal point

/** Process a string with a number plus a decimal point. There is no 0 before it, and two digits are retained. There is a method for loop Truncation on the Internet. If the number is too long and memory is wasted, this method is designed based on memory optimization. */-(NSString *) getTheCorrectNum :( NSString *) tempString {// first judge whether the first digit is ., yes. add 0 if ([tempString hasPrefix :@". "]) {tempString = [NSString stringWithFormat: @" 0% @ ", tempString];} // calculate the length of the intercepted NSUInteger endLength = tempString. length; // judge whether the string contains. if ([tempString containsString :@". "]) {// get. location of <G id = "1"> [tempString rangeOfString :@". "]; NSLog (@" % lu ", pointRange. location); // judge. there are several NSUInteger f = tempString. length-1-pointRange. location; // if the value is greater than two digits, the string is truncated to retain two digits. if the value is smaller than two digits, the if (f> 2) {endLength = pointRange. location + 2 ;}/// convert tempString to a char array NSUInteger start = 0; const char * tempChar = [tempString UTF8String]; // traversal, remove the for (int I = 0; I <tempString. length; I ++) {if (tempChar [I] = '0') {start ++;} else {break ;}// if the first letter is. returns an if (tempChar [start] = '. ') {start --;} // calculate the length based on the final start position, and intercept the nsange range = {start, endLength-start}; tempString = [tempString substringWithRange: range]; return tempString ;}

 

Related Article

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.