2

I've just started a class in C Programming, and while I have some background knowledge in JAVA, I'm trying to transition to this programming language. I have a project where I have to round user's input from something like 1.3333 to only two decimal places.

What I have so far is this:

#include <stdio.h>
int main (void)
{
   //v is my variable for the value which the user will input
   //Declaring variable as floating
   float v; 

   printf("Enter your value: \n");
   scanf("%.2f", &v);
   v = 0;

   printf("The rounded version is: %.2f");

   return 0;
}

This is what I have so far based off of what I've read in my book and this link: Rounding Number to 2 Decimal Places in C which my question is different from because it involves user input. My professor does say that I can't use a library function and need to use simple type casts to calculate it. This makes me feel that what I have might be wrong. Would the #include <stdio.h> be considered a library function? Given this information, is my thought process on the right track? If not, then would I do something like divide by variable by 100? Maybe %e for scientific notation?

Thanks ahead of time! Only asking for specific information, not coding or anything. Really want to understand the "hows" and "whys".

Community
  • 1
  • 1
J. Nav
  • 55
  • 2
  • 9
  • `scanf` is a library function. But not sure what your prof means by "use simple type casts". Type casting doesn't seem like the right way to solve this. At least, not just type casting on its own. Your idea of using basic math operations seems more on track. – kaylum Jan 18 '17 at 23:37
  • 3
    That `scanf("%.2f", &v);` is a format error. Please use `scanf("%f", &v);` and print it like you did to round to 2 dec places. Floating point variables themselves have no concept of decimal places. – Weather Vane Jan 18 '17 at 23:39
  • @Chimera It's not. I edited my question as to how it's not. I already looked in depth at that link and did not find the answer to my question. Mine involves user input which is why I felt it was reasonable to be posted. – J. Nav Jan 18 '17 at 23:44
  • 1
    The line `v = 0;` destroys the input to `v` from `scanf`. – Weather Vane Jan 18 '17 at 23:45
  • 1
    @J.Nav It doesn't matter how the code gets the number to be rounded. This question is still about how to round a float. It is therefore a duplicate. – Chimera Jan 18 '17 at 23:46
  • @J.Nav, are you sure you looked at it in depth? http://stackoverflow.com/questions/1343890/rounding-number-to-2-decimal-places-in-c#answer-1344260 has your answer. – anonymoose Jan 18 '17 at 23:46
  • @WeatherVane I thought I would use the %.2f because it would specify that I wanted the user's input to be rounded to only two decimal places. Is this something that is only appropriate in JAVA programming? – J. Nav Jan 18 '17 at 23:46
  • That does not round the input. `scanf` is similar in many ways to `printf` but is also different. As I said, it is a formatting error. The man page will be your friend as well as the links. – Weather Vane Jan 18 '17 at 23:47
  • @J.Nav If you read the answers in the other question you will surely find several workable answers. – Chimera Jan 18 '17 at 23:47
  • 1
    @Chimera It isn't a duplicate because it wasn't good enough for me. I wasn't sure how much the fact that I needed user input to be rounded changed the entire thing. Thank you for being concerned but I truly feel like mine is original in the fact that I gave my own coding, prompted my ideas, and listed the source I had previously been to. – J. Nav Jan 18 '17 at 23:49
  • 1
    Where the input comes from (a file, user input, or a code assigned variable) is irrelevant. Rounding output is the same regardless of where the input is obtained. This is in fact a duplicate of the post you say it doesn't duplicate. – Ken White Jan 18 '17 at 23:50
  • Rounding user input is fruitless. Always work to the highest accuracy available. C floating point variables hold no information of decimals, or decimal places. – Weather Vane Jan 18 '17 at 23:51
  • @WeatherVane If you want to tell that to my professor, go ahead, but this was the assignment I was given. – J. Nav Jan 18 '17 at 23:54
  • If you may only use typecasts, then use these! I don't see any typecast. (Note that there is only one logical cast here, *away* from `float`.) – Jongware Jan 18 '17 at 23:54
  • you haven't actually got a the rounded value in a variable, you have only printed the variable with reduced precision, I think your prof. wants you to do the calculation explicitly. – Jasen Jan 18 '17 at 23:55
  • @KenWhite I'm totally done with this site. You guys have almost never given me an answer I can actually use, and every time I ask a question it is shut down as being a duplicate despite thoroughly searching for an answer to my specific question. The fact that you even asked that blows my mind given that I'm a complete beginner at this, and did not know that. I'll take it down. – J. Nav Jan 18 '17 at 23:56
  • @J.Nav I suspect the prof was asking you to round the *output* of the value that was entered. As I already said, rounding a floating point variable itself to decimal places will not work, because f.p. cannot represent most decimal fractions exactly anyway. – Weather Vane Jan 18 '17 at 23:56
  • @Jasen Thanks Jasen. I was unsure but now I'm starting to see what it is that needs to be done. – J. Nav Jan 18 '17 at 23:57
  • 3
    I didn't *ask* you anything, so I don't know what question *blows your mind*. One of the things that new users here have difficulty learning is that this is not a site for posting a specific solution for **your exact situation**. This site is for questions that will help many people now and in the future, and it's *your* job to apply those previous answers to your specific case. In regards to **this question**, the other post tells you **exactly** how to **round the output**, and you asked how to **round the output**, which makes it a duplicate. – Ken White Jan 19 '17 at 00:10
  • To make it more clear: If a user asks *How do I add the int vars b and c in the C language?*, it's not a different post than *How do I add A and B in the C language?* simply because the variables are named differently. The answer to both questions are the same, making the second a duplicate. – Ken White Jan 19 '17 at 00:12
  • Oh god. Please just one more close vote. – Chimera Jan 19 '17 at 00:16
  • @KenWhite Then I'll go to a different site. – J. Nav Jan 19 '17 at 00:20
  • 4
    OK. Sorry to see you go. Good luck with that; there aren't many good sites out there (which is why we try to keep the noise and clutter down here). – Ken White Jan 19 '17 at 00:41

3 Answers3

2

First of all #include is a command that you need in order to include and use function that c provides for example for scanf you need to include library.

To round the number in two decimals without using %.2f in scanf you could write:

int x= (v*1000);
if(x%10>6) x=x/10+1 ;
else x= x/10;
printf("%d.%d",x/100,x%100);
coder
  • 12,832
  • 5
  • 39
  • 53
  • But I can't use library functions, so does that mean I can't use things like scanf? – J. Nav Jan 18 '17 at 23:52
  • 2
    You can't read else....What your professor means is to calculate the rounded number without libraries ... – coder Jan 18 '17 at 23:53
1

I think your professor aims not so much in user input but rather in understanding what happens when converting basic datatypes. Rounding, or at least cutting off digits, without library functions could look as follows:

int main (void)
{
    //v is my variable for the value which the user will input
    //Declaring variable as floating
    float v;

    printf("Enter your value: \n");
    scanf("%f", &v);
    v = float((int)(v*100))/100;

    printf("The rounded version is: %f", v);

    return 0;
}

Input/Output:

Enter your value: 
1.3333333
The rounded version is: 1.330000
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • It does use mathematic formulas, but where did you get the int? I only have one variable and it's v. V cannot be an integer since it will have decimal places. – J. Nav Jan 18 '17 at 23:53
  • This is wrong, firstly for 13.334 it gives 13.33 which is cutting digits not rounding!! and it also prints more digits since it is float.. e.g 13.3300000 – coder Jan 18 '17 at 23:55
  • you should probably use long instead of int. but if user enters 9e19 it will still give wrong answer. – Jasen Jan 18 '17 at 23:57
  • @coder so you would definitely recommend doing something with mathematics? But how can I read the user's input without the scanner and therefore the library functions? I guess with a loop? – J. Nav Jan 19 '17 at 00:02
  • @J.NAV, YOU CAN'T is the answer, to take the input from keyboard you need a system call to your os which is done by scanf ,you can't read without libraries, again the point here is to read a float and then without libraries round it... – coder Jan 19 '17 at 00:04
  • @coder: you are right concerning "rounding", of course. But I'm not sure at what the assignment aims, and if it is really about rounding in the sense that 1.349 should yield 1.35; And I'd neglect the issue with the trailing `0`, too. But who knows :-) – Stephan Lechner Jan 19 '17 at 00:06
  • @Chimera The theory of it is, sure, but the fact is that I have no idea where they got the int. I need my program to accept a floating value which then needs to be printed in a rounded format. Which makes me think I need to declare the variable and then get the input, then use math formulas to round it. Then printf the rounded value. – J. Nav Jan 19 '17 at 00:06
  • @Stephan Lechner , I think that since is it asked for rounding then 1.349 should be 1.35 .... – coder Jan 19 '17 at 00:08
  • See the edited answer; the 'int' is just an intermediate value, not even a variable, for calculating the result – Stephan Lechner Jan 19 '17 at 00:08
  • @coder So when he says, "accepts an arbitrary floating point value from standard input", I don't actually need to have user input? I just need to input my own value that gets rounded? I've emailed him for clarification but he hasn't gotten back to me yet. – J. Nav Jan 19 '17 at 00:09
  • I was taught that `1.330000` implies the accuracy is to 6 decimal places. If you are rounding `1.333333` to 2 decimal places, the answer is `1.33`. – Weather Vane Jan 19 '17 at 00:10
  • @J.NAV, No you need to have as input a floating number and the only way is by using a library function like scanf.... but then you need to calculate the rounded number by doing some mathematics.... – coder Jan 19 '17 at 00:12
  • @WeatherVane That's correct. Unless you are doing stats, then when you have a value where the next place over is 5 or higher, then you always round up. In this instance, it'd just be left at 1.33 like you said. – J. Nav Jan 19 '17 at 00:12
  • @coder What you're saying makes complete sense, thank you. He does say I cannot use library functions when it comes to computing the value. Which means I should be able to use them to get the actual value, but not for the rounding itself. Thank you for your patience. – J. Nav Jan 19 '17 at 00:14
  • @J.Nav glad to help!!! – coder Jan 19 '17 at 00:15
  • ...and of course you'll need a library function to see the output anyway, unless you are expected to view variables with a debugger. – Weather Vane Jan 19 '17 at 00:15
  • @WeatherVane My professor said debuggers are useless so no. – J. Nav Jan 19 '17 at 00:18
  • 2
    I think you have now told all about your "professor" that is needed. Just try to work past him/her. – Weather Vane Jan 19 '17 at 00:23
  • 1
    debuggers are in deed useless if it's not about programming – Stephan Lechner Jan 19 '17 at 00:27
1

Here is a working example that rounds properly without using any library calls other than stdio.

#include <stdio.h>

int main (void)
{
    float v; 

    printf("Enter your value: \n");
    scanf("%f", &v);


    v = (float)((int)(v * 100 + .5) / 100.0);

    printf("The rounded version is: %f\n",v);

    return 0;
}

Output:

jnorton@mint18 ~ $ ./a.out 
Enter your value: 
3.456
The rounded version is: 3.460000
Chimera
  • 5,884
  • 7
  • 49
  • 81
  • Thank you for this example. It's very helpful. From what I understand, I can't do something like "%.2f" because it's a Java thing. So I have to use multiplication to make sure the decimal place is correct. Still unsure as to why you would multiply the float by the int. – J. Nav Jan 19 '17 at 00:17
  • @J.Nav It first turns 3.456 into 346. Then divides by 100.0 to get it back into a float. – Chimera Jan 19 '17 at 00:19