How can I take a floating point number just one digit after the decimal point (0.1) using scanf in C language ? For example user may type 3.25405... but in variable only 3.2 will be saved . How can I do this ? This is different from rounding floating point number .
Asked
Active
Viewed 5,862 times
0
-
3Try to enter `0.1` and see the result in a debugger. You might be surprised. – too honest for this site Jul 27 '15 at 14:24
-
3Generally, don't ever read user input via `scanf()`. That function just does not offer enough capabilities to recover gracefully from error conditions, like format mismatches etc.. Read a whole line of input, then parse it in-memory (e.g. via `strtod()`). – DevSolar Jul 27 '15 at 14:33
-
You probably cannot do it in one step: scanf (nor fscanf or any variant, neither the underlying strtod) function does not have any provision to perform partial interpretation of the decimal part, it will try to match as much digits as possible. – AntoineL Jul 27 '15 at 14:33
-
How exactly do you intend on using this value once you have it? If you're always working with a single decimal point, you may be better off using an `int` in a fixed point format. – dbush Jul 27 '15 at 14:45
-
Unless you explain why it would be different to rounding a floating point number, I will vote to close this question as a duplicate. – Serge Ballesta Jul 27 '15 at 14:46
-
@sashoalm This is not a duplicate. OP does not want a round value. OP wants `3.25405` --> `3.2`, not `3.3`. – chux - Reinstate Monica Jul 27 '15 at 14:48
-
@Ridowan Ahmed Why does code need this behavior? Is it an _error_ to enter more than 1 digit after the `.`? Ignoring extra input seems like a problem. – chux - Reinstate Monica Jul 27 '15 at 14:57
3 Answers
1
I don't think you can do that in one pass, as scanf does not have, like printf does, the 'precision' option. So instead I would do:
int m, n;
float f;
scanf("%d.%1d", &m, &n);
if (m >= 0)
f=(float)m + ((float)n)/10;
else
f=(float)m - ((float)n)/10;
Hope it helps.
I have fixed the initial solution based on commebts below

Sorin Papuc
- 101
- 1
- 6
-
I would have used double rather than float for the result, as scanf does... – AntoineL Jul 27 '15 at 14:38
-
1) `n` & `m` reversed? 2) Suggest `long long` for the _whole_ part. 3) put together mindful of sign `f=whole + (whole < 0 ? -1 : 1)*fraction/10.0.` as the above fails when `whole < 0` 4) suggest `unsigned` fraction. – chux - Reinstate Monica Jul 27 '15 at 14:42
-
@chux yes, m and n reversed is a typo. I did not realize the sign issue though, thanks for pointing it out. – Sorin Papuc Jul 27 '15 at 14:56
-
I think it would be better to use `%d.%1%*d` or some variation on that, in order to skip over the ignored trailing digits. (At least, according to what appears to be the original request.) – rici Jul 27 '15 at 15:05
1
Read using fgets()
and post-process the buffer. @DevSolar
char buf[100];
if (fgets(buf, sizeof buf, stdin) == NULL) Handle_EOF();
char *p = strchr(buf,'.');
if (p && isdigit(p[1])) p[2] = 0;
char *endptr;
double y = strtod(buf, &endptr);
...
Even better to alert user of ignored additional input.
if (p && isdigit(p[1])) {
if (p[2] && p[2] != '\n') puts("Extra input ignored.");
p[2] = 0;
}
char *endptr;
double y = strtod(buf, &endptr);

Community
- 1
- 1

chux - Reinstate Monica
- 143,097
- 13
- 135
- 256
0
scanf("%3f",&float_var);
When the '3' is for the three characters of the 3.2 ('3' + '.' + '2') That, obviously, if you know that you are going to have a number with only one digit before the '.'
Cheers,

nerez
- 437
- 4
- 18