Thread: Please help, need user to decide how many decimal points to print.

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    2

    Please help, need user to decide how many decimal points to print.

    I am trying to have a user input a number, 5 for example, and then prints a number i have with that many decimal points.

    example:

    user types: 5

    prints: 1.10293

    (i chose random numbers here, but has 5 decimal points)

    I only know how to do this printf("%.5lf", mynumber);

    and that would print 5, but always 5.

    i want it to print the amount the user puts in.

    thank you for any help or advice
    Last edited by frezzy; 09-27-2017 at 09:53 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    There's sprintf that prints whatever you want into a string. Print the number you want, along with the format of course, into a string and pass it as the format string of printf.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    2
    Quote Originally Posted by GReaper View Post
    There's sprintf that prints whatever you want into a string. Print the number you want, along with the format of course, into a string and pass it as the format string of printf.
    hello thanks, I am sorry but I am very new at this and do not understand.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    sprintf - C++ Reference

    Use it kind-of like this:
    Code:
    char buffer[10];
    
    sprintf(buffer, "%%.%df", digits); // This produces "%.Nf", where N stands for the value held by 'digits'
    printf(buffer, mynumber); // This takes the string produced above as a format
    Devoted my life to programming...

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    @frezzy

    I think you were looking for this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
       double val = 12.3456789;
       int places = 0;
    
       if(argc != 2)
       {
          printf("USAGE: %s [decimal place int]\n", argv[0]);
          printf("Example: %s 2 (For two decimal places)\n", argv[0]);
          exit(EXIT_FAILURE);
       }
       else
       {
          places = atoi(argv[1]);
       }
    
       printf("%d Decimal places: %.*lf\n", places, places, val);
    
       return 0;
    }
    Use the '*' char to fill in the field width of the decimal portion in the format string, input by the user.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Oops, completely forgot about that specifier. Use @rstanley's solution, it's better for what you want to do.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal points
    By rocksolid in forum C Programming
    Replies: 4
    Last Post: 02-01-2014, 03:27 PM
  2. aligning decimal points?
    By mp91 in forum C Programming
    Replies: 2
    Last Post: 01-28-2013, 08:30 AM
  3. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-07-2002, 01:06 AM
  4. (C++) How to detect decimal points?
    By jeffcoulter in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2002, 02:36 PM
  5. decimal points
    By canine in forum Windows Programming
    Replies: 1
    Last Post: 04-29-2002, 10:01 PM

Tags for this Thread