Keep Two decimal places

Source: Internet
Author: User

/*
* Processing a string with a number plus a decimal point, preceded by 0, leaving two bits. There is a circular interception method on the Internet, if the number is too long and the memory is wasted, this method is designed on the basis of optimized memory.
*/
-(nsstring*) Getthecorrectnum: (nsstring*) tempstring
{
First judge is not. Is. Complement 0
if ([tempstring hasprefix:@ "."]) {
tempstring = [NSString stringwithformat:@ "0%@", tempstring];
}
Calculate the length of the Intercept
Nsuinteger endlength = tempstring.length;
Determines whether a string is contained.
if ([tempstring containsstring:@ "."]) {
Made. The location
Nsrange Pointrange = [tempstring rangeofstring:@ ".];
NSLog (@ "%lu", pointrange.location);
Judge. There are a few behind
Nsuinteger f = tempstring.length-1-pointrange.location;
If it is greater than 2 bits, the truncated string remains two bits, and if it is less than two bits, intercept directly
if (F > 2) {
Endlength = pointrange.location + 2;
}
}
Convert tempstring to char array first
Nsuinteger start = 0;
const char *tempchar = [tempstring utf8string];
Traverse, remove the position that gets the first digit not 0
for (int i = 0; i < tempstring.length; i++) {
if (tempchar[i] = = ' 0 ') {
start++;
}else {
Break
}
}
If the first letter is a. Start back one
if (tempchar[start] = = '. ') {
start--;
}
Calculates the length according to the final starting position, and intercepts
Nsrange range = {Start,endlength-start};
tempstring = [tempstring substringwithrange:range];
return tempstring;
}

Keep Two decimal places

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.