Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
int ptoi(char *percent){
char i=0;
int num=0;
while(percent[i]>='0'&&percent[i]<='9')
num=num*10+percent[i++]-0x30;
if(percent[i]=='.'){
if(percent[i+1]>='5'&&percent[i+1]<='9')
num++;
}
return(num);
}
int main()
{
printf("Percent 23.123 = %i\n",ptoi("23.123"));
printf("Percent 23.567 = %i\n",ptoi("23.567"));
return 0;
}
char *p;
p = strchr(string, '.');
if(p) {
*p = /0;
}
i = atoi(string);
However, it's a string not a float.the classic method for rounding a floating point number to integer is simply to add 0.5 to the float value and then truncate it to integer. No need to mess around with 'If" constructs.
This is a nice solution. Nice one MikeTry this, https://onlinegdb.com/SJyDRrYzN
Mike.
If that doesn't work,
Code:int ptoi(char *percent){ char i=0; int num=0; while(percent[i]>='0'&&percent[i]<='9') num=num*10+percent[i++]-0x30; if(percent[i]=='.'){ if(percent[i+1]>='5'&&percent[i+1]<='9') num++; } return(num); } int main() { printf("Percent 23.123 = %i\n",ptoi("23.123")); printf("Percent 23.567 = %i\n",ptoi("23.567")); return 0; }
Yup, I find in this forum in particular, sometimes the best solution is often ignored/brushed off for some reason.Normally code written specifically for a task is usually smaller and faster than using libraries. The code I posted above has the advantage of rounding and will definitely be more compact and faster than a float method and probably the itoa method as well.
Mike.