Daniel Zaksevski Daniel Zaksevski - 1 month ago 11
C Question

How to round float numbers in text in C?

So I have this text


today average human lifespan in Western countries is approaching and exceeding 80 years.

1 Japan 82.6 123 19

2 Hong Kong 82.2 234 411

3 Iceland 81.8 345 26

4 Switzerland 81.7 456 7

5 Australia 81.2 567 500

...

80 Lithuania 73.0 800 2

...

194 Swaziland 39.6 142 212

195 133714 18.0 133 998

196 100110011 10.0 667 87351


I need to round the float numbers in this text to whole numbers. I've searched a lot of forums but I cannot seem to find what I need.
This text is given in a .txt file. My task: "Round real number(that have decimals) to whole numbers. The corrected text should be in a new .txt" That's it.

Answer

here is the program I have already tested it for a single line of input string:-

// LINK - http://stackoverflow.com/questions/40317323/how-to-extract-float-numbers-from-text-in-c#40317323

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    char s[100];
    int i,j;
    double sum=0.0,frac=0.0;
    gets(s); // delimited by '\n' and not by ' '
    for( i=0;s[i]!='\0';i++) // for finding index value of decimal point(.)
    {     
        if(s[i]=='.')
            break;
    }

    for(;s[i]!=' ';i--); // for changing key index value to the index value of the first space character(' ') before the decimal point(.)
    i++;

    for(i;s[i]!='.';i++)
    {
        sum=sum*10+(s[i]-48); // For extracting integer part
    }

    i++;
    for(i,j=1;s[i]!=' ';i++,j++)
    {
        frac=frac+(s[i]-48)/pow(10,j); // For extracting fractional part
    }
    printf("\n\n%lf",sum+frac); // final answer integer part+decimal part
    return 0;
}
Explanation:-

okay so what I did is:-
Since scanf() is automatically delimited by space I used gets() which is delimited by new line;
Then we know that there will be a decimal point(.) for floating number in the string, so we find the index value(say key) of the decimal point in the array.
Once we have found the decimal point we know there will only be numbers between the decimal point and the space character after the country name so now we find the index value of that space(key).
Now from that key value we again first move to the decimal point(.) character index while calculating the integer part.Calculated using ASCII value i.e. ASCII value of character Zero(0) is 48 and character Nine(9) is 57 hence subtracting 48 from every character and extracting the value.
Again from the Decimal point(.) to the next space character in the string is part of the floating number and after decimal the numbers follow the weight 10^(-1),10^(-2),10^(-3)...and so on hence the temporary variable and the power() function.Thus we, successfully calculated the fractional part also.
Finally, I just added the Integer and the fractional parts within the printf().

Try, using printf() with all the variables in each of the for loops for better understanding, kinda like dry-run.

Comments