Hello.
The title pretty much says it all. I would like to know if and how you can print a number (type is double) with the same amount of decimal spaces as the one you entered.
If I enter 12.34 I would like the output to be 12.34 and not 12.340000000

Let's say that the program is this:

#include<stdio.h>
double a;

main(){

scanf("%lf",&a);
printf("%lf\n", a);

return 0;
}

Recommended Answers

All 14 Replies

Hello.
The title pretty much says it all.

Don't use the title as part of your problem description. The title is only to evoke enough interest for us to click on the thread. Then it's forgotten...

I would like to know if and how you can print a number (type is double) with the same amount of decimal spaces as the one you entered.

Read the number as a string. Count the decimals. Convert the string to your double.

double a;

include this in the main function

printf("%.2lf\n", a);

.(anynumber) represents the number of decimal places you want to output

or just do the solution posted by WaitP to be exact

commented: Doesnt come anywhere near the answer. -4

Like this? But how do I convert 2 strings to double? Need I use strcat?

#include<stdio.h>

char c[101],d[101];
main()
{
scanf("%100[^.].%s",c,d);
printf("%s.%s",c,d);


return 0;
}

Like this? But how do I convert 2 strings to double? Need I use strcat?

#include<stdio.h>

char c[101],d[101];
main()
{
scanf("%100[^.].%s",c,d);
printf("%s.%s",c,d);


return 0;
}

use atof

I don't really understand the example on the link. What's the division for? How would I use the function on one number?

Its just an example only for a sine calculator atof just basically changes a string to a float (double)
e.g.

n = atof (Input); //where n is a double and Input a string

I tried this. It doesn't work.

#include<stdio.h>
#include<string.h>

double a;
char c[101],d[101],e[1000],p[1];
main()
{
    p[0]='.';
scanf("%100[^.].%s",c,d);
printf("%s.%s\n",c,d);

strcat(e,c);
strcat(e,p);
strcat(e,d);
printf("%s\n",e);
a=strtod(e);
printf("%lf\n",a);
return 0;
}

Here's an example on using atof

#include<stdio.h>
#include<stdlib.h>
 
int main(void)
{
double a;
char c[10];
scanf("%s",c);

a=atof(c);
printf("%lf\n",a);
return 0;
}

Well, that is not even close. It turns string to float which was not my question. Output is supposed to be identical to input in terms of decimal spaces.

Well, that is not even close.

It's an example of how to convert a string to double, which is a necessary step in the solution to your problem.

Don't expect someone to do all of your work for you.

I don't need that kind of an example because I know how to use atof. I think the question is really simple. There is no "work" here. I'm not requiring a 1000-line code.

And also, I asked why doesn't the following code work.

#include<stdio.h>
#include<string.h>

double a;
char c[101],d[101],e[1000],p[1];
main()
{
    p[0]='.';
scanf("%100[^.].%s",c,d);
printf("%s.%s\n",c,d);

strcat(e,c);
strcat(e,p);
strcat(e,d);
printf("%s\n",e);
a=atof(e);
printf("%f\n",a);
return 0;
}

I don't need that kind of an example because I know how to use atof.

One of your posts suggested otherwise, which is why I suppose someone posted an example for you.

I think the question is really simple.

It is really simple. The first reply gave you the steps you need to perform: "Read the number as a string. Count the decimals. Convert the string to your double."

There is no "work" here.

Then why haven't you completed it yet? :D

And also, I asked why doesn't the following code work.

You forgot to include <stdlib.h>, and you're still printing a whole floating-point value. The value needs to be formatted correctly in the printf() format string, which is why you need to count digits initially:

printf("%.*f\n", precision, a);

Otherwise you don't know what value to use for precision . This is yet another case of misunderstanding the difference between internal and display representations.

Then why haven't you completed it yet?

I meant no work for someone who knows what to do. And if I were that person, I wouldn't be asking for help, now would I? :)

And yes, I said that I didn't understand the example I was given before because strtod was used in a different situation.

Anyway, thanks for the help.

The following code does the trick. Is there a simpler way to do this(less strings, without strcat)?

Also, could you explain what does %.*f actually mean? I mean, * is usually used for pointers.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

double a;
char c[101],d[101],e[1000],p[1];
main()
{
    p[0]='.';
scanf("%100[^.].%s",c,d);
printf("%s.%s\n",c,d);

strcat(e,c);
strcat(e,p);
strcat(e,d);
size_t dec=strlen(d);
printf("%s\n",e);
a=atof(e);
printf("%.*f\n",dec,a);
return 0;
}

Is there a simpler way to do this(less strings, without strcat)?

I'd do it more like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char buf[BUFSIZ];
    
    printf("Enter a floating-point value: ");
    fflush(stdout);
    
    if (fgets(buf, sizeof buf, stdin) != NULL) {
        double value;
        char *end;
        
        /* Trim the newline, if any */
        buf[strcspn(buf, "\n")] = '\0';
        
        /* Validate the string, we'll also need the value later */
        value = strtod(buf, &end);
        
        if (*end == '\n' || *end == '\0') {
            char *radix = strrchr(buf, '.');
            size_t precision = 0;
            
            if (radix != NULL)
                precision = strlen(radix + 1);
            
            printf("%.*f\n", precision, value);
        }
    }
    
    return 0;
}

That's not necessarily simpler, but it's more robust.

Also, could you explain what does %.*f actually mean? I mean, * is usually used for pointers.

In a printf() format string, * is a placeholder for some part of the field width. So %*d would print an integer where the field width is specified by another argument:

// These two calls are functionally identical
printf("%5d\n", 123);
printf("%*d\n", 5, 123);

See this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.